如何在matplotlib中的子图之间共享次要y轴

皮吉:

如果您有多个包含次级y轴的子图(使用twinx创建),那么如何在子图中共享这些次级y轴?我希望它们以自动方式平均缩放(因此,以后不要手动设置y限制)。对于主y轴,这是可能的,通过使用所述关键字sharey中的呼叫的插曲

下面的示例显示了我的尝试,但未能共享两个子图的辅助y轴。我正在使用Matplotlib / Pylab:

ax = []

#create upper subplot
ax.append(subplot(211))
plot(rand(1) * rand(10),'r')

#create plot on secondary y-axis of upper subplot
ax.append(ax[0].twinx())
plot(10*rand(1) * rand(10),'b')

#create lower subplot and share y-axis with primary y-axis of upper subplot
ax.append(subplot(212, sharey = ax[0]))
plot(3*rand(1) * rand(10),'g')

#create plot on secondary y-axis of lower subplot
ax.append(ax[2].twinx())
#set twinxed axes as the current axes again,
#but now attempt to share the secondary y-axis
axes(ax[3], sharey = ax[1])
plot(10*rand(1) * rand(10),'y')

这使我像:

共享次Y轴失败的两个子图的示例

我使用axes()函数设置共享y轴的原因是twinx不接受sharey关键字。

我在Win7 x64上使用Python 3.2。Matplotlib版本是1.2.0rc2。

dmcdougall:

您可以这样使用Axes.get_shared_y_axes()

from numpy.random import rand
import matplotlib
matplotlib.use('gtkagg')
import matplotlib.pyplot as plt

# create all axes we need
ax0 = plt.subplot(211)
ax1 = ax0.twinx()
ax2 = plt.subplot(212)
ax3 = ax2.twinx()

# share the secondary axes
ax1.get_shared_y_axes().join(ax1, ax3)

ax0.plot(rand(1) * rand(10),'r')
ax1.plot(10*rand(1) * rand(10),'b')
ax2.plot(3*rand(1) * rand(10),'g')
ax3.plot(10*rand(1) * rand(10),'y')
plt.show()

在这里,我们只是将辅助轴连接在一起。

希望能有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将Matplotlib子图与共享的X轴合并

如何在Matplotlib中强制Y轴仅使用整数?

如何在matplotlib中的子图之间共享次要y轴

在matplotlib中的次要y轴上添加y轴标签

在matplotlib中的次要y轴上添加y轴标签

如何在tickplabel和matplotlib中的轴之间添加空格?

在Matplotlib,Python中删除某些子图的y轴刻度标签

如何将子图应用于具有次要Y轴的图列表

Matplotlib子图y轴刻度与上面的图重叠

次要Y轴位置matplotlib

如何在R中创建金字塔条形图,在条之间具有y轴标签

次要Y轴上的Matplotlib Picker事件

如何在matplotlib中添加额外的y轴标签

Matplotlib共享y轴-一个子图未对齐

如何在子图之间共享色标尺?

如何在matplotlib中自动缩放条形图的y轴?

如何使用辅助y轴显示每个matplotlib子图的x轴值

当共享x和y轴时,如何为每个单独的图子图添加x和y标签?

在所有子图中使用次要连续y轴绘制子图

如何在matplotlib中缩小X轴上的图?

如何在带有子图的matplotlib中绘制x轴固定的2个动画图形?

在多个 matplotlib 子图上共享两个 y 轴

Matplotlib 子图:轴共享的相等轴

使用地图在子图之间共享 x 和 y 轴

为生成的子图共享轴 matplotlib

如何在 matplotlib 中移动轴标签、移动子图和修改轴缩放

在 matplotlib 子图中共享 Y 轴

如何在 Altair visual 中的 Y 轴和条形图之间添加更多空间

如何在 matplotlib 中更新 y 轴