Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw multiple subgraphs by Python Matplotlib

Shulou Source: shulou.com Published: 2022-06-01 12:57:37 11月11日 Update

This article will explain in detail how Python Matplotlib draws multiple subgraphs. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.

Merge legends by getting labels and linetypes for subplots

Note Add label

#Import data (readers can ignore) pre_lp=total_res#Combination model true=diff1[-pre_day:]#True value pre_ph=results_data["yhat"]#prophetpre_lstm=reslut#lstmpre_ari=data_ari <$'data_pre']#arima#Set Chinese font rcParams <$'font.sans-serif'] = 'kaiti'#Generate a time series (readers can modify or delete according to the situation) time =pd.to_datetime(np.arange(0,21), unit='D', origin=pd.Timestamp ('2021 -10- 19')#Create canvas fig=plt.figure (figsize=(20,16))#figsize is canvas size # 1 ax1= fig. add_subplot(221)ax1.plot (time,pre_lp,color='#1bb9f6',marker='^',linestyle='-',label='1')# ax1.plot (time,true,color='#fd5749',marker='s',linestyle='-',label='true')ax1.set_title ('1', fontsize=15)#Set title ax1.set_xlabel ('Date/day', fontsize=15)#Set abscissa name ax1.set_ylabel ('Infected people/person', fontsize=15)#Set ordinate name ax1.xaxis.set_major_formatter (mdate.DateFormatter ('% m-% d'))#Set abscissa scale (readers can ignore) plt.xticks (pd.date_range(time[0],time[-1],freq='D '),rotation=45)#Set abscissa scale (readers can ignore)# 2 ax2=fig.add_subplot(222)ax2.plot (time,pre_ph,color='#739b06',marker='o',linestyle='-',label='2')# ax2.plot (time,true,color='#fd5749',marker='s',linestyle='-',label='true')ax2.set_title ('2',fontsize=15)ax2.set_xlabel ('Date/day', fontsize=15)ax2.set_ylabel ('infected/person', fontsize=15)ax2.xaxis.set_major_formatter (mdate.DateFormatter('%m-%d'))plt.xticks (pd.date_range(time[0],time[-1],freq='D'),rotation=45)# 3 ax3=fig.add_subplot(223)ax3.plot (time,pre_lstm,color='#38d9a9',marker='*',linestyle='-',label='3')# ax3.plot (time,true,color='#fd5749',marker='s',linestyle='-',label='true')ax3.set_title ('3',fontsize=15)ax3.set_xlabel ('Date/day', fontsize=15)ax3.set_ylabel ('infected/person', fontsize=15)ax3.xaxis.set_major_formatter (mdate.DateFormatter('%m-%d'))plt.xticks (pd.date_range(time[0],time[-1],freq='D'),rotation=45)# 4 ax4=fig.add_subplot(224)ax4.plot (time,pre_ari,color='#e666ff',marker='x',linestyle='-',label='4')ax4.plot (time,true,color='#fd5749',marker='s',linestyle='-',label='true')ax4.set_title ('4',fontsize=15)ax4.set_xlabel ('Date/day', fontsize=15)ax4.set_ylabel ('infected/person', fontsize=15)ax4.xaxis.set_major_formatter (mdate.DateFormatter ('% m-% d'))plt.xticks(pd.date_range(time[0],time[-1],freq='D '),rotation=45)#Initialize labels and linetype arrays lines=[]labels=[]#Get linetypes and labels for ax in fig.axes by looping: axLine, axLabel = ax.get_legend_handles_labels() lines.extend(axLine) labels.extend(axLabel)#Set legend and adjust legend position fig.legend(lines, labels,loc='lower center', ncol=5,framealpha=False,fontsize=25)

The results are shown below

At this time, we will comment out the label and line type in the original code. The code is as follows:

#Import data (readers can ignore) pre_lp=total_res#Combination model true=diff1[-pre_day:]#True value pre_ph=results_data["yhat"]#prophetpre_lstm=reslut#lstmpre_ari=data_ari <$'data_pre']#arima#Set Chinese font rcParams <$'font.sans-serif'] = 'kaiti'#Generate a time series (readers can modify or delete according to the situation) time =pd.to_datetime(np.arange(0,21), unit='D', origin=pd.Timestamp ('2021 -10- 19')#Create canvas fig=plt.figure (figsize=(20,16))#figsize is canvas size # 1 ax1= fig. add_subplot(221)ax1.plot (time,pre_lp,color='#1bb9f6',marker='^',linestyle='-',label='1')ax1.plot (time,true,color='#fd5749',marker='s',linestyle='-',label='true')ax1.set_title ('1', fontsize=15)#Set title ax1.set_xlabel ('Date/day', fontsize=15)#Set abscissa name ax1.set_ylabel ('Infected people/person', fontsize=15)#Set ordinate name ax1.xaxis.set_major_formatter (mdate.DateFormatter ('% m-% d'))#Set abscissa scale (readers can ignore) plt.xticks (pd.date_range(time[0],time[-1],freq='D '),rotation=45)#Set abscissa scale (readers can ignore)# 2 ax2=fig.add_subplot(222)ax2.plot (time,pre_ph,color='#739b06',marker='o',linestyle='-',label='2')ax2.plot (time,true,color='#fd5749',marker='s',linestyle='-',label='true')ax2.set_title ('2',fontsize=15)ax2.set_xlabel ('Date/day', fontsize=15)ax2.set_ylabel ('infected/person', fontsize=15)ax2.xaxis.set_major_formatter (mdate.DateFormatter('%m-%d'))plt.xticks (pd.date_range(time[0],time[-1],freq='D'),rotation=45)# 3 ax3=fig.add_subplot(223)ax3.plot (time,pre_lstm,color='#38d9a9',marker='*',linestyle='-',label='3')ax3.plot (time,true,color='#fd5749',marker='s',linestyle='-',label='true')ax3.set_title ('3',fontsize=15)ax3.set_xlabel ('Date/day', fontsize=15)ax3.set_ylabel ('infected/person', fontsize=15)ax3.xaxis.set_major_formatter (mdate.DateFormatter('%m-%d'))plt.xticks (pd.date_range(time[0],time[-1],freq='D'),rotation=45)# 4 ax4=fig.add_subplot(224)ax4.plot (time,pre_ari,color='#e666ff',marker='x',linestyle='-',label='4')ax4.plot (time,true,color='#fd5749',marker='s',linestyle='-',label='true')ax4.set_title ('4',fontsize=15)ax4.set_xlabel ('Date/day', fontsize=15)ax4.set_ylabel ('infected/person', fontsize=15)ax4.xaxis.set_major_formatter (mdate.DateFormatter ('% m-% d'))plt.xticks(pd.date_range(time[0],time[-1],freq='D '),rotation=45)#Initialize labels and linetype arrays # lines=[]# labels=[]#Get linetypes and labels# for ax in fig.axes:# axLine, axLabel = ax.get_legend_handles_labels()# lines.extend(axLine)# labels.extend(axLabel)#Set legend and adjust legend position fig.legend(lines, labels,loc='lower center', ncol=5,framealpha=False,fontsize=25)

The results are shown below

Adjust subgraph spacing

plt.subplots_adjust(wspace=0.4,hspace=0.4)

wspace is the wide spacing between subgraphs, hspace is the high spacing between subgraphs

The comparison chart is as follows

Images with spacing set

Images with no set spacing

About "Python Matplotlib how to draw multiple subgraphs" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Number of people date reader Abscissa linetype legend spacing scale name canvas article loop adjustment multi-child Chinese font between code location image size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple Docker Huawei Microsoft