有没有办法在matplotlib子图中添加第二个y轴?

4

我认为我了解matplotlib中twiny/x函数,但我真的很难弄清如何在subplot上下文中使用此函数。我有一个线性图像这样显示降雨数据:

enter image description here

由此代码生成:

fig,ax = plt.subplots(3, figsize=(10,15),sharey=True)

ax[0].plot(YEAR1['pcp_1D_tot'], label='RG')
ax[0].plot(YEAR1['ppt_1D'], label='TRMM')
ax[0].set_title('Year 1',x=0.1,y=0.9)

ax[1].plot(YEAR2['pcp_1D_tot'], label='RG')
ax[1].plot(YEAR2['ppt_1D'], label='TRMM')
ax[1].set_title('Year 2',x=0.1,y=0.9)
ax[1].set_ylabel('Rainfall total (mm/day)')

ax[2].plot(YEAR3['pcp_1D_tot'], label='RG')
ax[2].plot(YEAR3['ppt_1D'], label='TRMM')
ax[2].set_title('Year 3',x=0.1,y=0.9)
ax[2].set_xlabel('Date')

fig.legend(loc=(0.8,0.9))
fig.tight_layout()
plt.show()

但我还有关于洪水大小的数据要添加,分为1、2、3级,存储在名为“例如”的列中。

YEAR1['Size'] 

我想将它们作为散点图绘制在线图的顶部,以显示它们相对于降雨量的发生率,因此我认为需要在右侧添加另一个y轴,但我非常不清楚如何做到这一点。
有人能帮忙吗?
**###########更新############### **
由于下面的贡献,我设法制作了以下内容,这正是我所希望的:

enter image description here

通过使用以下代码:
x = YEAR1m.index #### these are referring to other data that has been filtered
y = YEAR2m.index
z = YEAR3m.index

fig,ax = plt.subplots(3, figsize=(10,15),sharey=True)

ax[0].plot(YEAR1['pcp_1D_tot'], label='RG')
ax[0].plot(YEAR1['ppt_1D'], label='TRMM')
ax[0].set_title('Year 1',x=0.1,y=0.9)
ax0 = ax[0].twinx()
ax0.scatter(x, YEAR1m['Size'], marker='*', color='r',s=100)
ax0.set_ylim([0,3.2])
ax0.set_yticklabels(['0',' ','1',' ','2',' ','3'])


ax[1].plot(YEAR2['pcp_1D_tot'], label='RG')
ax[1].plot(YEAR2['ppt_1D'], label='TRMM')
ax[1].set_title('Year 2',x=0.1,y=0.9)
ax[1].set_ylabel('Rainfall total (mm/day)')
ax1 = ax[1].twinx()
ax1.scatter(y, YEAR2m['Size'], marker='*', color='r',s=100)
ax1.set_ylim([0,3.2])
ax1.set_yticklabels(['0',' ','1',' ','2',' ','3'])



ax[2].plot(YEAR3['pcp_1D_tot'], label='RG')
ax[2].plot(YEAR3['ppt_1D'], label='TRMM')
ax[2].set_title('Year 3',x=0.1,y=0.9)
ax[2].set_xlabel('Date')
ax2 = ax[2].twinx()
ax2.scatter(z, YEAR3m['Size'], marker='*', color='r',s=100)
ax2.set_ylim([0,3.2])
ax2.set_yticklabels(['0',' ','1',' ','2',' ','3'])


# fig.legend(loc=(0.8,0.9))
fig.tight_layout()
plt.show()

请提供一些样本数据以便我们进行工作。谢谢。 - jose_bacoy
2个回答

3
在没有数据的情况下,我只能提供一个猜测解决方案。以下内容应该可行。您需要使用单个子图对象,使用 twinx 创建次要 y 轴。
fig,ax = plt.subplots(3, figsize=(10,15),sharey=True)

ax[0].plot(YEAR1['pcp_1D_tot'], label='RG')
ax[0].plot(YEAR1['ppt_1D'], label='TRMM')
ax[0].set_title('Year 1',x=0.1,y=0.9)

ax0 = ax[0].twinx()
ax0.plot(YEAR1['Size'])


ax[1].plot(YEAR2['pcp_1D_tot'], label='RG')
ax[1].plot(YEAR2['ppt_1D'], label='TRMM')
ax[1].set_title('Year 2',x=0.1,y=0.9)
ax[1].set_ylabel('Rainfall total (mm/day)')

ax1 = ax[1].twinx()
ax1.plot(YEAR2['Size'])

ax[2].plot(YEAR3['pcp_1D_tot'], label='RG')
ax[2].plot(YEAR3['ppt_1D'], label='TRMM')
ax[2].set_title('Year 3',x=0.1,y=0.9)
ax[2].set_xlabel('Date')

ax2 = ax[2].twinx()
ax2.plot(YEAR3['Size'])

在Matplotlib 3.1中,还有一个新的secondary_axes方法。 - Jody Klymak

2

我没有你的数据,让我们看看我是否理解了你的问题...

通常的事情,

import matplotlib.pyplot as plt 
import numpy as np 

生成一些数据。
x = np.linspace(0, 1.57, 21) 
y = np.sin(x) 
z = 4*np.random.random(21) 

准备带有两个子图的图表 - 我将只使用第一个子图以避免让您感到无聊 - 添加寄生轴而不是当前绘图,但添加到特定轴。
fig, (ax0, ax1) = plt.subplots(2, 1) 
ax01 = ax0.twinx() 

绘制曲线,使用不同颜色绘制散点图(这不会自动处理)。
ax0.plot(x, y, color='blue') 
ax01.scatter(x, z, color='red') 

这是一个不太被请求的最后一步处理...(注意,是 colors,而不是 color)

ax0.tick_params(axis='y',  colors='blue') 
ax01.tick_params(axis='y', colors='red')                                         

最后,plt.show() 给我们呈现出:

enter image description here

我想补充一点:感谢Rutger Kassies提供的精彩答案,在我的回答中,读者可以找到有关自定义两个垂直脊柱所有细节的进一步建议。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接