Dearis
Dearis

Reputation: 143

return values of subplot

Currently I trying to get myself acquainted with the matplotlib.pyplot library. After having seeing quite some examples and tutorial, I noticed that the subplots function also has some returns values which usually are used later on. However, on the matplotlib website I was unable to find any specification on what exactly is returned, and none of the examples are the same (although it usually seems to be an ax object). Can you guys give me some to pointers as to what is returned, and how I can use it. Thanks in advance!

Upvotes: 13

Views: 15042

Answers (3)

Akash Manna
Akash Manna

Reputation: 1

Actually, 'matplotlib.pyplot.subplots()' is returning two objects:

  1. The figure instance.
  2. The 'axes'.

'matplotlib.pyplot.subplots()' takes many arguments. That has been given below:

matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)

The first two arguments are : nrows : the number of rows I want to creat in my Subplot grid , ncols : The number of columns should have in the subplot grid. But, if 'nrows' and 'ncols' are not decleared explicitely, it will take the values of 1 in each by default.

Now, come to objects that has been created: (1)The figure instance is nothing but throwing a figure which will hold all the plots.

(2)The 'axes' object will contain all the informations about each subplots.

Let's understand through an example:

enter image description here

Here, 4 subplots are being created at the positions of (0,0),(0,1),(1,0),(1,1).

Now, let's suppose, at the position (0,0), I want to have a scatterplot. What will I do: I will incorporate the scatterplot into "axes[0,0]" object that will hold all the informations about the scatterplot and reflect it into the figure instance. The same thing will happen for all the other three positions.

Hope this will help and let me know your thought about this.

Upvotes: 0

kumabis4u
kumabis4u

Reputation: 51

Generally, the matplotlib.pyplot.subplots() returns a figure instance and an object or an array of Axes objects.

Since you haven't posted the code with which you are trying to get your hands dirty, I will do it by taking 2 test cases :

case 1 : when number of subplots needed(dimension) is mentioned

import matplotlib.pyplot as plt #importing pyplot of matplotlib 
import numpy as np
x = [1, 3, 5, 7]
y = [2, 4, 6, 8]
fig, axes = plt.subplots(2, 1)
axes[0].scatter(x, y)
axes[1].boxplot(x, y)
plt.tight_layout()
plt.show()

enter image description here

As you can see here since we have given the number of subplots needed, (2,1) in this case which means no. of rows, r = 2 and no. of columns, c = 1. In this case, the subplot returns the figure instance along with an array of axes, length of which is equal to the total no. of the subplots = r*c , in this case = 2.

case 2 : when number of subplots(dimension) is not mentioned

import matplotlib.pyplot as plt #importing pyplot of matplotlib 
import numpy as np
x = [1, 3, 5, 7]
y = [2, 4, 6, 8]
fig, axes = plt.subplots() 
#size has not been mentioned and hence only one subplot
#is returned by the subplots() method, along with an instance of a figure
axes.scatter(x, y)
#axes.boxplot(x, y)
plt.tight_layout()
plt.show()

enter image description here

In this case, no size or dimension has been mentioned explicitly, therefore only one subplot is created, apart from the figure instance.

You can also control the dimensions of the subplots by using the squeeze keyword. See documentation. It is an optional argument, having default value as True.

Upvotes: 3

RickardSjogren
RickardSjogren

Reputation: 4238

In the documentation it says that matplotlib.pyplot.subplots return an instance of Figure and an array of (or a single) Axes (array or not depends on the number of subplots).

Common use is:

import matplotlib.pyplot as plt
import numpy as np
f, axes = plt.subplots(1,2)  # 1 row containing 2 subplots.

# Plot random points on one subplots.
axes[0].scatter(np.random.randn(10), np.random.randn(10))

# Plot histogram on the other one.
axes[1].hist(np.random.randn(100))

# Adjust the size and layout through the Figure-object.
f.set_size_inches(10, 5)
f.tight_layout()

Upvotes: 19

Related Questions