Matplotlib Main Components
Matplotlib is a Python library widely used for data visualization. This is a brief summary about basic components of this library.
Figure
It is the area of the figure, it controls all the sub-structures and elements that are within its area.
fig = plt.figure(figsize=[7,2], facecolor='lightgreen') #Create only a figure
fig.suptitle('Example Figure 1') #Figure title
ax = fig.add_subplot() #Create axes through subplot creatingIn code above the figure is created in the first line, the Axes are only created in the third line of code when the subplot is created. The Figure element is represented by the green area in the second image.
#Create a figure containing a single axes
fig, axs = plt.subplots(figsize=(7, 2), facecolor='lightblue')
fig.suptitle('Example Figure 2') #Figure title
plt.show()In this second code, the Axes are created together with the figure in the same line of code. The Figure element is represented by the blue area in the second image.
Below are some parameters and methods related to component ‘Figure’:
Figsize: The size of the figure on the display is defined by the ‘figsize’ parameters, with the first one being width and the second one being height.
Layout: It is possible to define the layout of the figure using the ‘layout’ parameter and choose between ‘constrained’, ‘compressed’, ‘tight’ or none(default). It resizes the figure to avoid overlapping substructures and elements, and can avoid the need for manual adjustments.
Facecolor: The ‘facecolor’ parameter defines the background color of the figure, which is white by default.
Suptitle: It adds a legend to the figure, a title.
Get Carlacosmo’s stories in your inbox
Join Medium for free to get updates from this writer.
Subfigures: It is a method used to add subfigures to a figure.
#Create only a figure
fig = plt.figure(figsize=[8,4], layout='constrained', facecolor='#CBC3E3')
fig.suptitle('Figure') #Figure title
figL, figR = fig.subfigures(1, 2) #Create 2 subfigures
figL.set_facecolor('lightgreen') #Change color
axL = figL.subplots(2, 1) #Create 2 subplots
figL.suptitle('Left Subfigure') #Subfigure title
figR.set_facecolor('lightblue') #Change color
axR = figR.subplots(1, 3) #Create 3 subplots
figR.suptitle('Right Subfigure') #Subfigure title
plt.show()
fig.savefig('MyFigure.png', dpi=200)Figures can be saved as files using the ‘savefig’ method. If you are having any problems viewing your graph ‘plt.show()’ is a function used at the end of the code to display the entire figure without any problem.
Axes
It is the area where the data will be plotted, it includes all plot and sub-plot elements. Axes is not instantiated directly, but uses Axes creation methods ‘figure’ and ‘pyplot’ (subplots).
Add_subplot: It is the method used to create each Axes.
fig = plt.figure(figsize=[5,2], layout='constrained', facecolor='#CBC3E3')
fig.suptitle('Figure')
axL = fig.add_subplot()
axL.set_xlabel('x')
axL.set_ylabel('y')
plt.show()Axis
Define the scale and limit for data on the axis with ticks and labels in Axes.Usually contains 2 axis, only in cases where the 3D graph will contain 3 axis.It also has a title on each axis (x and y axis).
Set_xlabel: defines a label for the x-axis.
Set_ylabel: defines a label for the y-axis.
Artist
Everything visible in the figure is an artist, even the Figure, Axes and Axis.
Reference: https://matplotlib.org/stable/index.html









