When python loops to draw subgraphs, set the shared x and y axes
00 Preface
Generally, when reading the literature, it is found that when drawing some pictures, if there are multiple subgraphs, usually in order to be beautiful and concise, only the scale of the last column and the leftmost subgraph are retained, as shown in the following figure:
So, how should we realize this function?
Generally speaking, there are two main functions to realize multi subgraph drawing in python:
- The first is through fig.add_ Subplot (row, column, position) adding
import matplotlib.pyplot as plt fig=plt.figure(figsize=(5,5)) subplot=fig.add_subplot(1,1,1)
- The second is through PLT Subplots (rows and columns), the number of rows and columns are passed to the method as parameters. The method returns a graph object and axis object, which can be used to operate the graph.
import matplotlib.pyplot as plt fig,ax=plt.subplots(2,1)
1. Use. Fig_ Subplot (row, column, position) add a subplot
Among them, the first one is relatively easy to use. You only need to specify the specific location of the subgraph, as shown in the following figure:
import matplotlib.pyplot as plt fig=plt.figure(figsize=(8,6),dpi=100) ax_1=fig.add_subplot(121) ax_1.text(0.3, 0.5, 'subplot(121)') ax_2=fig.add_subplot(222) ax_2.text(0.3, 0.5, 'subplot(222)') ax_3=fig.add_subplot(224) ax_3.text(0.3, 0.5, 'subplot(224)') fig.suptitle("Figure with multiple Subplots") plt.show()
1.1 cycle drawing subgraph
If you want to display a 2x2 four subgraph on a page, you only need to add a cycle:
import matplotlib.pyplot as plt fig=plt.figure(figsize=(8,6),dpi=100) c=0 for i in range(4): c=c+1 ax=fig.add_subplot(2,2,c) ax.text(0.3, 0.5, 'subplot(22'+str(c)+')') fig.suptitle("Figure with multiple Subplots") plt.show()
2. Use PLT Subplots (row, column) add a subplot
matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
However, PLT Subplots() and fig.add_subplot() is a little more troublesome than that, but it also has more functions. You can draw subplots by returning the number of subplots in the ax list:
import matplotlib.pyplot as plt fig,ax=plt.subplots(1,2,dpi=300) ax[0].text(0.3,0.5,"1st Subplot") ax[0].set_xticks([]) ax[0].set_yticks([]) ax[1].text(0.3,0.5,"2nd Subplot") ax[1].set_xticks([]) ax[1].set_yticks([]) fig.suptitle('Figure with 2 subplots',fontsize=16) plt.show()
2.1 cycle drawing subgraph
For PLT Subplots(), if you want to draw subgraphs circularly, you need double loops:
import matplotlib.pyplot as plt fig, axes = plt.subplots(2, 2, figsize=(6,6),dpi=100) for i, row in enumerate(axes): for j, col in enumerate(row): axes[i,j].text(0.3, 0.5, 'axes['+str(i)+','+str(j)+']') plt.tight_layout()
3. Sets the shared axis when you cycle through the subgraphs
Then, how to draw subgraphs circularly and realize the shared coordinate axis at the same time? Here we need to use PLT Subplots (), which provides a parameter to automatically realize the function of sharing coordinate axes. You only need to add sharex and sharey when creating the number of subgraphs, as shown below:
fig, axes = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(6,6))
Here is a simple demonstration:
import matplotlib.pyplot as plt fig, axes = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(6,6),dpi=100) for i, row in enumerate(axes): for j, col in enumerate(row): axes[i,j].text(0.3, 0.5, 'axes['+str(i)+','+str(j)+']') fig.suptitle('Figure with 2x2 subplots',fontsize=16) plt.tight_layout()
It can be found that our needs have been realized!!
4. Set shared label when drawing subgraphs circularly
Similarly, if you want to realize shared label, what should you do?
There are actually two ideas:
- Pass ax Text() select the appropriate position to add
- When drawing a subgraph, add a judgment statement
Here is a simple demonstration:
import matplotlib.pyplot as plt fig, axes = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(6,6),dpi=100) for i, row in enumerate(axes): for j, col in enumerate(row): axes[i,j].text(0.3, 0.5, 'axes['+str(i)+','+str(j)+']') fig.suptitle('Figure with 2x2 subplots',fontsize=16) fig.text(0.5, 0, 'xlabel', ha='center') fig.text(0, 0.5, 'ylaebl', va='center', rotation='vertical') plt.tight_layout()
Alternatively, it can be as follows:
import matplotlib.pyplot as plt fig, axes = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(6,6),dpi=100) for i, row in enumerate(axes): for j, col in enumerate(row): axes[i,j].text(0.3, 0.5, 'axes['+str(i)+','+str(j)+']') fig.suptitle('Figure with 2x2 subplots',fontsize=16) if col.is_last_row(): col.set_xlabel('xlabel') if col.is_first_col(): col.set_ylabel('ylaebl') plt.tight_layout()
Tips:
-
For the drawing of ocean and atmosphere related majors, projection needs to be added generally, so it needs to be added when generating the number of subgraphs
One studies hard python of ocean er Limited level, welcome to correct!!! Welcome to comment, collect, like, forward and follow. Pay attention to me without regret and record the process of learning progress~~