matplotlib多个图组成子图的画法
程序员文章站
2022-03-20 23:52:03
...
import numpy as np
import matplotlib.pyplot as plt
MSDC_real = np.loadtxt('MSDC_real.txt') #import data
MSDC_random = np.loadtxt('random_MSDC_results.txt') #import data
#set font property
font1 = {'weight': 'bold',
'size': 24
}
#define plot function, I prefer to use the function to plot, it easy to make revision
def MSDC_plot2():
x = MSDC_real[:,0] #import x data from the first column of array MSDC_real
y1= MSDC_real[:,1] #import y1 data from the second column of array MSDC_real
y2 = MSDC_real[:,2] #same as above
y2_error = MSDC_real[:,3] #same as above
fig = plt.figure(figsize=(12,18)) #set figure size
#define the ax1 and plot first subplot
ax1 = fig.add_subplot(211)
ax1.bar(x, y1, width=0.1)
ax1.set_ylim(0,180)
ax1.set_xlim(-4.8,-2.7)
ax1.set_ylabel('Number', fontweight='bold', fontsize=36)
plt.tick_params(direction='in')
plt.yticks(size = 30)
#plot vertical marking lines
x_ver1 = [-3.9,-3.9]
y_ver1 = [0,200]
x_ver2 = [-3.5,-3.5]
y_ver2 = [0,200]
x_ver3 = [-2.9,-2.9]
y_ver3 = [0,200]
ax1.plot(x_ver1,y_ver1,color='gray',linewidth=3.0,linestyle='dashed')
ax1.plot(x_ver2,y_ver2,color='gray',linewidth=3.0,linestyle='dashed')
ax1.plot(x_ver3,y_ver3,color='gray',linewidth=3.0,linestyle='dashed')
ax = plt.gca() #get current axis
ax.spines['left'].set_linewidth(2.0)
ax.spines['right'].set_linewidth(2.0)
ax.spines['top'].set_linewidth(2.0)
ax.spines['bottom'].set_linewidth(2.0)
#plot second subplot
ax2 = fig.add_subplot(212) # this is the important function
ax2.errorbar(x,y2,y2_error,fmt='s', color='darkorange', label='MSDC', ms=12, capsize=6)
ax2.set_ylabel('Log(MSDC) (deg$^{-2}$)', fontweight='bold', fontsize=36)
ax2.set_xlabel('Log($\\theta$) (deg)', fontweight='bold', fontsize=36)
ax2.tick_params(direction='in')
plt.xticks(fontproperties = 'Times New Roman', size = 30)
plt.yticks(fontproperties = 'Times New Roman', size = 30)
ax2.plot(x_ver1,y_ver1,color='gray',linewidth=3.0,linestyle='dashed')
ax2.plot(x_ver2,y_ver2,color='gray',linewidth=3.0,linestyle='dashed')
ax2.plot(x_ver3,y_ver3,color='gray',linewidth=3.0,linestyle='dashed')
#plot MSDC radom data:
y_center = MSDC_random[:,1]
y_up = MSDC_random[:,2]
y_down = MSDC_random[:,3]
#plot center line:
ax2.plot(x,y_center,color='black', linewidth=3.0,label='MSDC random')
#plot error area of random MSDC
ax2.fill_between(x,y_up,y_down, facecolor='lightgrey', label='MSDC random errors')
ax2.set_ylim(4.5,9.5)
ax2.set_xlim(-4.8,-2.7)
ax2.legend(prop=font1)
#plot linear fitting:
x_fit1 = [-4.75,-4.25]
y_fit1 = [-2.0368 * x_fit1[0] - 0.7698,-2.0368 * x_fit1[1] - 0.7698]
ax2.plot(x_fit1,y_fit1,color='red',linestyle='dashed',linewidth=3.0)
#plot vertical line:
x_vertical = [-4.25,-4.25]
y_vertical = [4,10]
plt.plot(x_vertical,y_vertical,color='tan',linewidth=3.0,linestyle='dashed')
#adjust line width of axes
ax = plt.gca() #get current axis
ax.spines['left'].set_linewidth(2.0) #adjust the linewidth of axes "left, right, top, and bottom"
ax.spines['right'].set_linewidth(2.0)
ax.spines['top'].set_linewidth(2.0)
ax.spines['bottom'].set_linewidth(2.0)
plt.tight_layout(h_pad=-1.7) #adjust the panel to fill the whole figure, h_pad = -value can be used to adjust the gap between two subplots.
#plt.show()
plt.savefig('MSDC.eps') #save figure
MSDC_plot2()
效果图如下所示:
上一篇: 求函数【线段树】【2020牛客寒假算法基础集训营2】
下一篇: Python爬虫学习日记一 爬取
推荐阅读