具有2个y轴的matplotlib图 [英] matplotlib diagrams with 2 y-axis

查看:77
本文介绍了具有2个y轴的matplotlib图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于时间线图的 matplolib 中,我可以将左侧的 y 轴设置为不同的值,并使用其他比例在右侧制作另一个 y 轴吗?

In matplolib for a time line diagram can I set to y-axis different values on the left and make another y-axis to the right with other scale?

我正在使用这个:

import matplotlib.pyplot as plt 

plt.axis('normal')
plt.axvspan(76, 76, facecolor='g', alpha=1)
plt.plot(ts, 'b',linewidth=1.5)
plt.ylabel("name",fontsize=14,color='blue')
plt.ylim(ymax=100)
plt.xlim(xmax=100)
plt.grid(True)
plt.title("name", fontsize=20,color='black')
plt.xlabel('xlabel', fontsize=14, color='b')
plt.show()

我可以在这个图中给出 2 个 y 轴吗?

Can I give 2 y-axis in this plot?

跨度选择器:

 plt.axvspan(76, 76, facecolor='g', alpha=1)

我想对文本进行正确的描述以表示此跨度,例如这是跨度选择器",我该如何制作?

I want to right text to characterize this span for example 'This is span selector' how can I make it?

推荐答案

您要 twinx 示例.要点是:

ax = plt.gca()
ax2 = ax.twinx()

然后您可以使用

ax.plot(...)

和第二个

ax2.plot(...)

在你的情况下(我认为)你想要:

In your case (I think) you want:

import matplotlib.pyplot as plt 

ax = plt.gca()
ax2 = ax.twinx()
plt.axis('normal')
ax2.axvspan(74, 76, facecolor='g', alpha=1)
ax.plot(range(50), 'b',linewidth=1.5)
ax.set_ylabel("name",fontsize=14,color='blue')
ax2.set_ylabel("name2",fontsize=14,color='blue')
ax.set_ylim(ymax=100)
ax.set_xlim(xmax=100)
ax.grid(True)
plt.title("name", fontsize=20,color='black')
ax.set_xlabel('xlabel', fontsize=14, color='b')
plt.show()

这篇关于具有2个y轴的matplotlib图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆