且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-21 16:48:58

您可以像这样使用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()

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

Here we're just joining the secondary axes together.

希望有帮助.