python如何画出多个独立的图片,使用pyplot在python中的多个子图上绘制水平线

I am plotting three subplots on the same page. I want to draw a horiZontal line through all the subplots. Following is my code and the resultant graph: (You can notice I can get the horizontal line on one of the plots, but not all)

gs1 = gridspec.GridSpec(8, 2)

gs1.update(left=0.12, right=.94, wspace=0.12)

ax1 = plt.subplot(gs1[0:2, :])

ax2 = plt.subplot(gs1[3:5, :], sharey=ax1)

ax3 = plt.subplot(gs1[6:8, :], sharey=ax1)

ax1.scatter(theta_cord, density, c = 'r', marker= '1')

ax2.scatter(phi_cord, density, c = 'r', marker= '1')

ax3.scatter(r_cord, density, c = 'r', marker= '1')

ax1.set_xlabel('Theta (radians)')

ax1.set_ylabel('Galaxy count')

ax2.set_xlabel('Phi (radians)')

ax2.set_ylabel('Galaxy count')

ax3.set_xlabel('Distance (Mpc)')

ax3.set_ylabel('Galaxy count')

plt.ylim((0,0.004))

loc = plticker.MultipleLocator(base=0.001)

ax1.yaxis.set_major_locator(loc)

plt.axhline(y=0.002, xmin=0, xmax=1, hold=None)

plt.show()

This generates the following:

S9rEm.png

Again, I want the line I drew on the last subplot to appear on the first two subplots too. How do I do that?

解决方案

I found a way to do it for anyone who stumbles on this anyways.

We need to replace the following line from the OP:

plt.axhline(y=0.002, xmin=0, xmax=1, hold=None)

We replace it with:

ax1.axhline(y=0.002,xmin=0,xmax=3,c="blue",linewidth=0.5,zorder=0)

ax2.axhline(y=0.002,xmin=0,xmax=3,c="blue",linewidth=0.5,zorder=0)

ax3.axhline(y=0.002,xmin=0,xmax=3,c="blue",linewidth=0.5,zorder=0)

This produces:

wfPOS.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值