Matplotlib,图例,带有一个标签的多个不同标记 [英] Matplotlib, legend with multiple different markers with one label
问题描述
这是情况的简化图:
我希望每个数据点标记都具有一个单独的标签,而我希望能够为一组不同的标记使用一个标签.我希望能够有一个像这样的传说:
Instead of have each data point marker have a separate label, I want to be able to have one label for a set of different markers. I would like to be able to have a legend like:
<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点/组)标记/标签组合会很杂乱.
I want to do this because in the final plot I will have three sets of data points and showing 18 (3 sets * 6 points/set) marker/label combinations will be messy.
我正在将Matplotlib与Python 2.7结合使用.
I am using Matplotlib with Python 2.7.
推荐答案
注意:保留此答案作为正确答案的指南-但这不能解决所陈述的问题.有关matplotlib不支持补丁集合的问题,请参见下面的编辑.
Note: This answer is kept up as a guide to the correct answer - but it does not solve the stated problem. Please see the edit below for the problem of Patch collections not being support in matplotlib.
如果要完全自定义,一种解决方法是使用所谓的代理艺术家:
One way to go about this, if you want complete customization is to use a so-called Proxy Artist:
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()
在这里,您可以精确地指定 您想要图例的外观,甚至可以使用非标准的绘图形状(补丁).
Here you can specify exactly what you'd like the legend to look like, and even use non-standard plot shapes (patches).
There is some difficulty with this method, matplotlib only supports these Artists in a legend without tweaking. For matplotlib v1.0 and earlier, the supported artists are as follows.
Line2D
Patch
LineCollection
RegularPolyCollection
CircleCollection
您的多个分散点请求将通过补丁集合,上面不支持.从理论上讲,对于v1.1,这是可行的,但我不知道如何做到.
Your request of multiple scatter points would be done with a Patch Collection, which is not supported above. In theory with v1.1, this is possible but I do not see how.
这篇关于Matplotlib,图例,带有一个标签的多个不同标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!