Matplotlib, a legend with several different markers with the same label

Here is a simplified diagram of the situation:

plot example

Instead of each data point marker having a separate label, I want to have one label for a set of different markers. I would like 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 on the final graph I will have three sets of data points and showing 18 (3 sets * 6 points / sets) markers / label combinations will be messy.

I am using Matplotlib with Python 2.7.

+8
python matplotlib
source share
1 answer

Note. This answer is saved as a guide for the correct answer, but it does not solve the stated problem. Please see the description of the problem below when collecting patches is not supported in matplotlib.


One way to do this is if you want the full setup to use the 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 how you want the legend to look, and even use non-standard forms (patches).

Edit: There are some difficulties with this method, matplotlib supports these artists only in legend without configuration. For matplotlib v1.0 and previously supported artists are as follows.

 Line2D Patch LineCollection RegularPolyCollection CircleCollection 

Your request for multiple scatter points will be done using the patch collection , which is not supported above. Theoretically, with v1.1 this is possible, but I do not see how to do it.

+3
source share

All Articles