matplotlib多个图组成子图的画法

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()

效果图如下所示:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值