首页 文章

Matplotlib,带有一个标签的多个不同标记的图例

提问于
浏览
9

以下是情况的简化情节:

plot example

我希望能够为一组不同的标记设置一个标签,而不是让每个数据点标记都有一个单独的标记 . 我希望能有一个像这样的传奇:

<triangle> <square> <hexagon> <diamond> <circle> Shotcrete strength data points
<green line> 10 minute strength
<blue line> 60 minute strength
<yellow line> 1 day strength
<orange line> 7 day strength
<red line> 28 day strength

我想这样做是因为在最终的情节中我会有三组数据点并显示18(3组* 6点/组)标记/标签组合将是混乱的 .

我在Python 2.7中使用Matplotlib .

1 回答

  • 3

    注意:这个答案可以作为正确答案的指南 - 但它并不能解决上述问题 . 请参阅下面的编辑,了解在matplotlib中不支持补丁集合的问题 .


    解决此问题的一种方法是,如果您想要完全自定义,请使用所谓的代理艺术家:

    from pylab import *
    
    p1 = Rectangle((0, 0), 1, 1, fc="r")
    p2 = Circle((0, 0), fc="b")
    p3 = plot([10,20],'g--')
    legend([p1,p2,p3], ["Red Rectangle","Blue Circle","Green-dash"])
    
    show()
    

    在这里,您可以准确指定图例的外观,甚至可以使用非标准图形(修补程序) .

    Edit: 这种方法存在一些困难,matplotlib only supports these Artists in a legend没有调整 . 对于matplotlib v1.0及更早版本,支持的艺术家如下 .

    Line2D
    Patch
    LineCollection
    RegularPolyCollection
    CircleCollection
    

    您对多个散点的请求将使用Patch Collection完成,上面不支持 . 理论上与v1.1,这是可能的,但我不知道如何 .

相关问题