Using Matplotlib in Python to Set Multiple Subgraph Methods

Method 1:

Use plt. subplots () to uniformly add multiple subgraphs

import matplotlib.pyplot as plt
#set a 2 * 2, size 6 x 6. clarity is 100, background color is white, shared x related to y subgraph of axis 
# fig it is a graphic object that represents the entire graphic window, where multiple drawing areas can be placed. 
# axes it's a two-dimensional numpy array, containing N subgraph objects can be drawn within their respective regions 
fig,axes = plt.subplots(nrows=2,ncols=2,figsize=(6,6),dpi=100,facecolor='w',sharex=True,sharey=True)
#add data labels to subgraphs 
# zip the () function packages the elements in two list objects into tuples corresponding to each other 
for a,b in zip(['a','b','c','d'],[1,2,3,4]):
axes[0,0].text(a,b,b)
for a,b in zip(['a','b','c','d'],[5,2,3,4]):
axes[0,1].text(a,b,b)
#draw for each subgraph 
axes[0,0].plot(['a','b','c','d'],[1,2,3,4])
axes[0,1].plot(['a','b','c','d'],[5,2,3,4])
#add partial titles to each subgraph 
axes[0,0].set_title('I Am Title')
axes[0,1].set_title('I Am Title')
#add global title 
fig.suptitle('My Multi subplot Figure')
plt.show()

Method 2:

Enable fig.add_Subplot() specifies the position to add a subgraph

fig = plt.figure(figsize=(6,6),dpi=100,facecolor='w')
# add_subplot () yes figure a method that can apply to existing figure add in 
ax1 = fig.add_subplot(2,2,1)  #the first position of a two-dimensional matrix 
ax1.plot(['a','b','c','d'],[1,2,3,4])
ax1.set_title('title')
ax2 = fig.add_subplot(2,2,4)
ax2.plot(['a','b','c','d'],[1,2,3,4])  #the fourth position of a two-dimensional matrix 
ax2.set_title('title')
plt.show()

Method 3:

Use plt. subplot() to specify the location to add a subgraph

fig = plt.figure(figsize=(6,6),dpi=100,facecolor='w')
# plt.subplot () yes matplotlib the built-in function can be used in the current figure create subgraph on top 
plt.subplot(221)
plt.plot(['a','b','c','d'],[1,2,3,4])
plt.title('title A')
plt.subplot(223)
plt.plot(['a','b','c','d'],[1,2,3,4])
plt.title('title C')
plt.show()

Plt. subplot() is suitable for quickly creating subgraphs.

4. Fine tuning of multiple subgraphs

As shown in the figure below, the spacing between subgraphs is too small, so we can use subplots_Adjust() to adjust the spacing.

fig = plt.figure(figsize=(6,6),dpi=100,facecolor='w')
plt.subplot(221)
plt.plot(['a','b','c','d'],[1,2,3,4])
plt.title('title A')
plt.subplot(222)
plt.plot(['a','b','c','d'],[1,3,8,6])
plt.title('title B')
plt.subplot(223)
plt.plot(['a','b','c','d'],[1,2,3,4])
plt.title('title C')
plt.subplot(224)
plt.plot(['a','b','c','d'],[6,5,1,4])
plt.title('title D')
#the first four parameters are the distance between the subgraph and the edge of the parent window, wspace and hspace horizontal and vertical spacing between subgraphs, respectively 
fig.subplots_adjust(left=0.1,right=0.9,top=0.8,bottom=0.1,wspace=0.3,hspace=0.5)
fig.suptitle('Big Title',fontsize=20)
plt.show()

5. Summary

Plt. subplots () and fig.add_Sublot() is suitable for situations that require more precise control (as obtaining subgraph objects), while plt. subplot() is suitable for quickly creating subgraphs.

Using Fig.subplots_Adjust() adjusts the spacing, and when setting parameters, it is important to note that left/bottom cannot be greater than or equal to right/top.