Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib, legend with multiple different markers with one label

Here is a simplified plot of the situation:

plot example

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

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.

I am using Matplotlib with Python 2.7.

like image 759
CrazyArm Avatar asked Feb 13 '12 14:02

CrazyArm


1 Answers

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).

Edit: 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

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.

like image 187
Hooked Avatar answered Sep 20 '22 12:09

Hooked