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

2024-04-26 18:15:37 发布

您现在位置:Python中文网/ 问答频道 /正文

(我想)我知道matplotlib中的twiny/x函数,但是我真的很难弄清楚如何在子批上下文中使用这个函数。我有一个像这样的线图显示降雨数据:

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

Tags: plottitlefigpltaxyearlabelrg
2条回答

在没有数据的情况下,我只能提供一个猜测的解决方案。下面的方法应该有效。您必须使用单个子图对象来使用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'])

我没有你的资料,让我看看我是否了解你的问题。。。你知道吗

平常的事

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谢谢你的fine answer,在这里,我的答案的读者可以找到关于定制两个垂直脊椎的所有细节的进一步建议。你知道吗

相关问题 更多 >