【问题标题】:plot nothing but the legend in matplotlib subplot只绘制 matplotlib 子图中的图例
【发布时间】:2013-04-05 20:24:00
【问题描述】:

我有一堆子图,其中一个我想包含一个图例(在每个其他子图中都有相同数量的句柄和标签,所以我只想要一个主图例)。

|plot|gap1|plot|
|plot|plot|plot|
|plot|gap2|plot|

现在gap1在哪里,我要放我的高手传奇。我在第一个子图中绘制了相同的图。

line1 = ax2.plot(x1,y2,'--',color='b',linewidth=2)
line2 = ax2.plot(x1,y2,'-',color='r')
ax2.legend((line1,line2),('line1','line2'),numpoints=1,loc='center')
l = line1[0]
l.remove()
l = line2[0]
l.remove()
ax2.set_axis_off()

但这似乎不起作用。

现在我只想将图例放在 ax2 子图的中心(在 gap1 中)并删除 所有内容(轴、线、刻度等)除了传说在中心。 谢谢。

【问题讨论】:

    标签: python matplotlib legend


    【解决方案1】:
    fig, axes_lst = plt.subplots(2, 1)
    
    ln_1, ln_2 = axes_lst[0].plot(range(5), range(5), range(5), np.arange(5) ** 2)
    axes_lst[1].legend((ln_1, ln_2), ('lin', 'quad'), loc='center')
    plt.draw()
    

    在调用 legend 时使用的句柄不必位于您调用 legendaxes 上。

    【讨论】:

    • 在 plt.draw() 之前添加 axes_lst[1].set_axis_off() 是我想要的。谢谢@tcaswell。
    • 我刚刚处理了你没有工作的部分;)。如果这解决了你的问题,你能接受吗?
    猜你喜欢
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2021-10-30
    • 2021-11-06
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2019-02-14
    相关资源
    最近更新 更多