【问题标题】:Share Y Axis across multiple Figures in Matplotlib在 Matplotlib 中跨多个图形共享 Y 轴
【发布时间】:2019-07-08 16:20:13
【问题描述】:

我的图形需要非常高分辨率,因此我不能使用多轴子图(导致 ValueError:102400x6400 像素的图像尺寸太大)。但是,我仍然希望所有数字共享 y 轴,就像使用 plt.subplots(sharey=True) 一样。在这种情况下,我不关心只显示一个 y 轴,就像子图的结果一样。相反,我希望刻度位于相同的位置,并且它们之间的距离相同。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    当然,您可以简单地手动为两个轴设置相同的限制。事实上,这将是我在产生高质量输出时推荐的解决方案。

    ax1.set_xlim(xmin, xmax)
    ax2.set_xlim(xmin, xmax)
    

    可以根据用例自动计算 xminxmax

    如果需要完全自动化的解决方案或需要真正的共享,您可以share axes after their creation

    import numpy as np
    import matplotlib.pyplot as plt
    
    x1 = np.arange(6)
    y1 = np.tile([1,2],3) 
    
    x2 = np.arange(5,11)
    y2 = np.tile([6,8],3) 
    
    
    fig1, ax1 = plt.subplots()
    fig2, ax2 = plt.subplots()
    
    ax1.plot(x1,y1)
    ax2.plot(x2,y2)
    
    ax1.get_shared_x_axes().join(ax1, ax2)
    ax1.get_shared_y_axes().join(ax1, ax2)
    
    ax2.autoscale()
    
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-21
      • 1970-01-01
      • 2021-05-12
      • 2012-10-06
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多