Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I am a little confused about how this code works:

fig, axes = plt.subplots(nrows=2, ncols=2)

How does the fig, axes work in this case? What does it do?

Also why wouldn't this work to do the same thing:

fig = plt.figure()

axes = fig.subplots(nrows=2, ncols=2)

1 Answer

0 votes
by (106k points)

For getting multiple subplots in matplotlib you can use the below-mentioned code:-

import matplotlib.pyplot as plt

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(range(10), 'r')

ax[1, 0].plot(range(10), 'b') 

ax[0, 1].plot(range(10), 'g')

ax[1, 1].plot(range(10), 'k')

enter image description here

To know more about this you can have a look at the following video tutorial:-

Related questions

Browse Categories