Legende mit PathCollections in matplotlib

Ich zeichne Gruppen von Kreisen mithilfe von Sammlungen und kann die Legende der drei Kategorien nicht generieren. Ich will:

Katze 1: rote KreiseKatze 2: blaue KreiseKatze 3: gelbe Kreise
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 sind keine iterierbaren Objekte. Versuchen Sie daher, die Legende folgendermaßen zu generieren.

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

funktioniert nicht.

Wie kann die Beschriftung erscheinen?

Mein System läuft unter Python 2.7 und Matplotlib 1.2.0_1

Beachten Sie, dass der Befehlprint p.get_label() Zeigt an, dass dem Objekt eine Beschriftung zugeordnet ist, matplotlib die Legende jedoch nicht bereitstellen kann.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage