【问题标题】:How to have a poligon in the legend如何在图例中有一个多边形
【发布时间】:2021-09-08 05:55:54
【问题描述】:

我有一个包含 4 个元素的图表:蓝色正方形、蓝色三角形、红色正方形和红色三角形, 我正在尝试将这 4 个项目添加到图例中。 当我向图例添加多边形补丁时,它显示为矩形,即使补丁是三角形 我怎样才能让它工作?

legend_elements = [patches.RegularPolygon((4, 4), 3, 0.5, np.pi / 4,label="Triangle")]
plt.legend(handles=legend_elements)

【问题讨论】:

    标签: python matplotlib legend polygon


    【解决方案1】:

    您可以使用markers 行来表示多边形:

    from matplotlib import pyplot as plt
    from matplotlib.lines import Line2D
    
    handles = [Line2D([0], [0], linestyle='none', mfc='blue', mec='blue', marker='s', label='blue square'),
               Line2D([0], [0], linestyle='none', mfc='blue', mec='blue', marker='^', label='blue triangle'),
               Line2D([0], [0], linestyle='none', mfc='red', mec='red', marker='s', label='red square'),
               Line2D([0], [0], linestyle='none', mfc='red', mec='red', marker='^', label='red triangle')]
    plt.legend(handles=handles)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 2011-01-23
      相关资源
      最近更新 更多