【问题标题】:Putting two different entries under same label in a legend将两个不同的条目放在图例中的同一标签下
【发布时间】:2020-07-09 10:46:52
【问题描述】:

我用不同的数据集和趋势线绘制了一个图表。我想知道是否可以在同一个标​​签下有两个不同的条目而标签在图例中出现两次:

ax.plot(JD_inc, mag_inc, 'x', label = "Increasing Magnitude")
ax.plot(JD_dec, mag_dec, 'x', color = "forestgreen", label = "Decreasing Magnitude")

ax.plot(new_x_inc, new_y_inc, color = "orange", label = "Increasing Magnitude")
ax.plot(new_x_dec, new_y_dec, color = "crimson", label = "Decreasing Magnitude")


ax.errorbar(our_jul_day, our_mag, yerr = our_err, fmt = "x", color = "black", zorder = 10, label = "Our Data")

plt.legend()

是否也可以从“我们的数据”标签中删除错误栏?

这是我当前图表的图像:

那么,例如,是否有可能让橙色线和蓝色点在图例中基本上重叠?

【问题讨论】:

    标签: python matplotlib plot graph legend


    【解决方案1】:

    根据link 中给出的HandlerTuple 示例,以下内容应该适用于您的情况:

    jd_inc_data, = ax.plot(JD_inc, mag_inc, 'x', label = "Increasing Magnitude")
    new_x_inc_data, = ax.plot(new_x_inc, new_y_inc, color = "orange", label = "Increasing Magnitude")
    
    plt.legend([(jd_inc_data, new_x_inc_data)], ["Increasing Magnitude"])
    

    【讨论】:

    • 这已经成功了,谢谢,但是当我尝试为他的 dec_data 复制它时,我得到了错误:'NoneType' object has no attribute 'create_artists'
    • 好吧,不用担心,我错过了变量名后面的逗号。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    相关资源
    最近更新 更多