return values of subplot

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.


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()