In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.