Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legend using PathCollections in matplotlib

I'm plotting groups of circles using collections and I am not able to generate the legend of the three categories. I want:

  • Cat 1: red circles
  • Cat 2: blue circles
  • Cat 3: yellow circles
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle
import numpy as np

# (modified from one of the matplotlib gallery examples)
resolution = 50 # the number of vertices
N = 50
Na = 25
Nb = 10
x        = np.random.random(N)
y       = np.random.random(N)
radii   = 0.1*np.random.random(30)

xa       = np.random.random(Na)
ya       = np.random.random(Na)
radiia   = 0.1*np.random.random(50)


xb       = np.random.random(Nb)
yb       = np.random.random(Nb)
radiib   = 0.1*np.random.random(60)

patches = []
patchesa = []
patchesb = []
for x1,y1,r in zip(x, y, radii):
     circle = Circle((x1,y1), r)
     patches.append(circle)

for x1,y1,r in zip(xa, ya, radiia):
    circle = Circle((x1,y1), r)
    patchesa.append(circle)

for x1,y1,r in zip(xb, yb, radiib):
    circle = Circle((x1,y1), r)
    patchesb.append(circle)


fig = plt.figure()
ax = fig.add_subplot(111)

colors = 100*np.random.random(N)
p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4, label= "Cat 1", facecolor="red")
pa = PatchCollection(patchesa, cmap=matplotlib.cm.jet, alpha=0.3, label= "Cat 2", facecolor="blue")
pb = PatchCollection(patchesb, cmap=matplotlib.cm.jet, alpha=0.4, label= "Cat 3", facecolor="yellow")
#p.set_array(colors)
ax.add_collection(p)
ax.add_collection(pa)
ax.add_collection(pb)
ax.legend(loc = 2)
plt.colorbar(p)

print p.get_label()

plt.show()

PathCollections are not iterable objects, so trying to generate the legend the following way;

legend([p, pa, pb], ["cat 1", "2 cat", "cat 3"])

does not work.

How can the caption to appear?

My system run on Python 2.7 and Matplotlib 1.2.0_1

Note that the command print p.get_label() shows that the object has an associated label, but matplotlib is unable to mount the legend.

like image 688
Daeron Avatar asked May 17 '13 17:05

Daeron


People also ask

How do I show a legend in MatPlotLib?

MatPlotLib with Python Set the figure size and adjust the padding between and around the subplots. Using plot() method, plot lines with the labels line1, line2 and line3. Place a legend on the figure using legend() method, with number of labels for ncol value in the argument. To display the figure, use show() method.

How do I create a multi legend in MatPlotLib?

MatPlotLib with PythonPlace the first legend at the upper-right location. Add artist, i.e., first legend on the current axis. Place the second legend on the current axis at the lower-right location. To display the figure, use show() method.

How do I change the font size in legend in MatPlotLib?

legend() to change the font size of a Matplotlib legend. Call matplotlib. pyplot. legend(list, prop={'size': new_size}) twith list as a list of labels and new_size as an integer to change the font size to new_size .


1 Answers

One possible solution is to add Line2D objects to use in the legend, also known as using proxy artists. To achieve this you have to add from matplotlib.lines import Line2D to your script, and then you can replace this code:

ax.legend(loc = 2)
plt.colorbar(p)

print p.get_label()

with this:

circ1 = Line2D([0], [0], linestyle="none", marker="o", alpha=0.4, markersize=10, markerfacecolor="red")
circ2 = Line2D([0], [0], linestyle="none", marker="o", alpha=0.3, markersize=10, markerfacecolor="blue")
circ3 = Line2D([0], [0], linestyle="none", marker="o", alpha=0.4, markersize=10, markerfacecolor="yellow")

plt.legend((circ1, circ2, circ3), ("Cat 1", "Cat 2", "Cat 3"), numpoints=1, loc="best")

enter image description here

like image 170
sodd Avatar answered Sep 24 '22 14:09

sodd