【问题标题】:Python Matplotlib - Secondary axis multiple legendsPython Matplotlib - 辅助轴多个图例
【发布时间】:2020-11-26 07:14:29
【问题描述】:

我有一个具有辅助轴的图。轴 1 绘制了两个数据集。轴 2 有一个数据集。 我可以得到两个图例(一个来自 Axis 1,一个来自 Axis 2),就像我想要的那样 - 一个在右侧的绘图之外的另一个下方。

我希望轴 1 的第二个数据集的图例低于上述两个图例。但它出现在两者之外。

我怎样才能让它工作?

下面是我的代码:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b-',label='data1')
ax1.set_xlabel('time (s)')
ax1.legend(loc='lower left', bbox_to_anchor= (1.1, 0.7), ncol=2,
            borderaxespad=0, frameon=False)

ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r',label='data2')
ax2.legend(loc='lower left', bbox_to_anchor= (1.1, 0.6), ncol=2,
            borderaxespad=0, frameon=False)

data3 = [10000]*len(t)
ax1.plot(t,data3,'k--',label='data3')
ax1.legend(loc='lower left', bbox_to_anchor= (1.1, 0.5), ncol=2,
            borderaxespad=0, frameon=False)

plt.show()

当我更改 bbox_to_anchor 的 y 值时,“data3”不会出现在与其他两个图例一起出现的列中,而是与两个图例中的任何一个一起显示在一行中。

谢谢

R

【问题讨论】:

    标签: python matplotlib legend


    【解决方案1】:

    ncol=2 更改为ncol=1 以将图例项限制在同一列。

    import numpy as np
    import matplotlib.pyplot as plt
    
    # constrained layout worked best for me, but you can change it back
    fig = plt.figure(constrained_layout=True)
    ax1 = fig.add_subplot(111)
    t = np.arange(0.01, 10.0, 0.01)
    s1 = np.exp(t)
    ax1.plot(t, s1, 'b-',label='data1')
    ax1.set_xlabel('time (s)')
    ax1.legend(loc='lower left', bbox_to_anchor= (1.1, 0.7), ncol=1,
                borderaxespad=0, frameon=False)
    
    ax2 = ax1.twinx()
    s2 = np.sin(2*np.pi*t)
    ax2.plot(t, s2, 'r',label='data2')
    ax2.legend(loc='lower left', bbox_to_anchor= (1.1, 0.6), ncol=1,
                borderaxespad=0, frameon=False)
    
    data3 = [10000]*len(t)
    ax1.plot(t,data3,'k--',label='data3')
    ax1.legend(loc='lower left', bbox_to_anchor= (1.1, 0.5), ncol=1,
                borderaxespad=0, frameon=False)
    
    plt.show()
    

    【讨论】:

      【解决方案2】:

      您可以使用线句柄和标签手动构建图例:

      import numpy as np
      import matplotlib.pyplot as plt
      
      fig = plt.figure()
      ax1 = fig.add_subplot(111)
      t = np.arange(0.01, 10.0, 0.01)
      s1 = np.exp(t)
      ax1.plot(t, s1, 'b-',label='data1')
      ax1.set_xlabel('time (s)')
      
      ax2 = ax1.twinx()
      s2 = np.sin(2*np.pi*t)
      ax2.plot(t, s2, 'r',label='data2')
      lh2, l2 = ax2.get_legend_handles_labels()
      
      data3 = [10000]*len(t)
      ax1.plot(t,data3,'k--',label='data3')
      lh1, l1 = ax1.get_legend_handles_labels()
      
      ax1.legend([lh1[0]]+lh2+[lh1[1]], 
                 [l1[0]]+l2+[l1[1]], 
                 loc='lower left', 
                 bbox_to_anchor= (1.1, 0.4), 
                 ncol=1,
                 borderaxespad=0, 
                 frameon=False)
      

      输出:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-09
        • 2021-03-22
        • 2022-06-28
        • 1970-01-01
        相关资源
        最近更新 更多