The matplotlib.figure is a top-level container for all the plot elements.
Parameters:
- figsize: figure dimension (width, height) in inches.
- dpi : Dots per inch.
- facecolor : The figure patch facecolor. default – White
- edgecolor : The figure patch edge color. Default – White
- linewidth : The linewidth of the frame
- frameon : bool (True/False) Defalut-True. If
False, suppress drawing the figure background patch. - subplotpars : Subplot parameters. if not given the default subplot parameters are used.
- tight_layout : bool or dict. Defalut-False.
-
- If
Falseuse subplotpars. - If
Trueadjust subplot parameters usingtight_layoutwith default padding. - When providing a dict containing the keys
pad,w_pad,h_pad, andrect, the defaulttight_layoutpaddings will be overridden.
- If
-
- constrained_layout : bool(True/False). Default-False.
-
- If True, use constrained layout to adjust the positioning of plot elements
-
. . .
Figure.add_subplot():
The figure.add_subplot() method add the subplot to figure. You can add subplot with different ways like:
add_subplot(nrows, ncols, index, **kwargs) add_subplot(pos, **kwargs) add_subplot(ax) add_subplot()
Parameters:
- Either a 3-digit integer or three separate integers describing the position of the subplot.
-
- In the above first syntax nrows, ncols and index define the position of the subplot.
- In the second syntax, pos defines the position of the subplot, which consists of 3 digit integer number, the first digit define the number of rows, the second digit defines the number of columns and the last digit define the index of the subplot.
-
Different ways to use add_subplot():
fig = plt.figure(figsize=(10,5)) fig.add_subplot(221) # equivalent but more general ax1 = fig.add_subplot(2, 2, 1) # add a subplot with no frame ax2 = fig.add_subplot(222, frameon=False) # add a red subplot that share the x-axis with ax1 fig.add_subplot(223, sharex=ax1, facecolor='red') fig.delaxes(ax2) # delete x2 from the figure fig.add_subplot(ax2) # add x2 to the figure again
Example
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12)
x = np.random.rand(10)
y = np.random.rand(10)
z = np.sqrt(x**2 + y**2)
fig = plt.figure(figsize=(10,8),facecolor='cyan')
fig.suptitle("Example of Subplot")
fig.add_subplot(221)
plt.scatter(x, y, s=80, c=z, marker=">")
plt.title("subplot-1")
fig.add_subplot(222)
plt.scatter(x, y, s=80, c=z, marker="+")
plt.title("subplot-2")
fig.add_subplot(223)
plt.scatter(x, y, s=80, c=z, marker='.')
plt.title("subplot-3")
fig.add_subplot(224)
plt.scatter(x, y, s=80, c=z, marker="*")
plt.title("subplot-4")
plt.show()
This produces the following result:

. . .
Figure.Subplots()
The figure.subplots() method add a set of subplots to the figure. This method is convenient to create common layouts of subplots in a single call.
Example. You can generate the same above plot using figure.subplots() method.
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12)
x = np.random.rand(10)
y = np.random.rand(10)
z = np.sqrt(x**2 + y**2)
fig = plt.figure(figsize=(10,8),facecolor='cyan')
axs = fig.subplots(2,2)
fig.suptitle("Example of Subplot")
axs[0,0].scatter(x, y, s=80, c=z, marker=">")
axs[0,0].set_title('subplot-1')
axs[0,1].scatter(x, y, s=80, c=z, marker="+")
axs[0,1].set_title('subplot-2')
axs[1,0].scatter(x, y, s=80, c=z, marker='.')
axs[1,0].set_title('subplot-3')
axs[1,1].scatter(x, y, s=80, c=z, marker="*")
axs[1,1].set_title('subplot-4')
plt.show()
. . .