Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do many examples use `fig, ax = plt.subplots()` in Matplotlib/pyplot/python

I'm learning to use matplotlib by studying examples, and a lot of examples seem to include a line like the following before creating a single plot...

fig, ax = plt.subplots() 

Here are some examples...

  • Modify tick label text
  • http://matplotlib.org/examples/pylab_examples/boxplot_demo2.html

I see this function used a lot, even though the example is only attempting to create a single chart. Is there some other advantage? The official demo for subplots() also uses f, ax = subplots when creating a single chart, and it only ever references ax after that. This is the code they use.

# Just a figure and one subplot f, ax = plt.subplots() ax.plot(x, y) ax.set_title('Simple plot') 
like image 783
neelshiv Avatar asked Dec 08 '15 17:12

neelshiv


People also ask

What does Fig ax PLT subplots () mean in Python?

subplots method provides a way to plot multiple plots on a single figure. Given the number of rows and columns , it returns a tuple ( fig , ax ), giving a single figure fig with an array of axes ax .

Why do we use subplots in matplotlib?

Subplots mean groups of axes that can exist in a single matplotlib figure. subplots() function in the matplotlib library, helps in creating multiple layouts of subplots. It provides control over all the individual plots that are created.

What are the PLT and ax in matplotlib exactly?

As shown in the above-annotated screenshot, when we draw a graph using plt : A Figure object is generated (shown in green) An Axes object is generated implicitly with the plotted line chart (shown in red) All the elements of the plot such as the x and y-axis are rendered inside the Axes object (shown in blue)

Why do we use PLT figures?

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc.


2 Answers

plt.subplots() is a function that returns a tuple containing a figure and axes object(s). Thus when using fig, ax = plt.subplots() you unpack this tuple into the variables fig and ax. Having fig is useful if you want to change figure-level attributes or save the figure as an image file later (e.g. with fig.savefig('yourfilename.png')). You certainly don't have to use the returned figure object but many people do use it later so it's common to see. Also, all axes objects (the objects that have plotting methods), have a parent figure object anyway, thus:

fig, ax = plt.subplots() 

is more concise than this:

fig = plt.figure() ax = fig.add_subplot(111) 
like image 173
jonchar Avatar answered Sep 27 '22 17:09

jonchar


Just a supplement here.

The following question is that what if I want more subplots in the figure?

As mentioned in the Doc, we can use fig = plt.subplots(nrows=2, ncols=2) to set a group of subplots with grid(2,2) in one figure object.

Then as we know, the fig, ax = plt.subplots() returns a tuple, let's try fig, ax1, ax2, ax3, ax4 = plt.subplots(nrows=2, ncols=2) firstly.

ValueError: not enough values to unpack (expected 4, got 2) 

It raises a error, but no worry, because we now see that plt.subplots() actually returns a tuple with two elements. The 1st one must be a figure object, and the other one should be a group of subplots objects.

So let's try this again:

fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(nrows=2, ncols=2) 

and check the type:

type(fig) #<class 'matplotlib.figure.Figure'> type(ax1) #<class 'matplotlib.axes._subplots.AxesSubplot'> 

Of course, if you use parameters as (nrows=1, ncols=4), then the format should be:

fig, [ax1, ax2, ax3, ax4] = plt.subplots(nrows=1, ncols=4) 

So just remember to keep the construction of the list as the same as the subplots grid we set in the figure.

Hope this would be helpful for you.

like image 30
Duskash Avatar answered Sep 27 '22 18:09

Duskash