How to share secondary y-axis between subplots in matplotlib

后端 未结 1 1328
慢半拍i
慢半拍i 2020-12-08 03:02

If you have multiple subplots containing a secondary y-axis (created using twinx), how can you share these secondary y-axis between the subplots? I want them to sca

相关标签:
1条回答
  • 2020-12-08 03:22

    You can use Axes.get_shared_y_axes() like so:

    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.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题