2016-04-02 9 views
0

Der Versuch, eine Legende zu meinem Konturplot hinzuzufügen: Python 3.5 matplotlib Konturplot Legende

Hier ist der relevante Code Teil i mit Problem habe:

plt.figure() 
CS = plt.contourf(gg, cc, zz_miss) 
CS.clabel() 
lbl = CS.cl_cvalues 
plt.xlabel('gamma') 
plt.ylabel('C = 1/lambda') 
plt.legend((lbl), loc= 'upper right') 

plt.show() 

Die Legende Etiketten der Legende sind richtig, aber warum sind die entsprechenden Farben verschmiert und fehl am Platz?

Antwort

2

Es setzt die Literalpolygone Ihres Konturplots als Markierungen. Es ist ein Problem.

Ich rate Ihnen, einen manuellen Ersatz für die Farblegende zu erstellen. Hier ist die Änderung müssen Sie in Ihren Code machen (erzeugten synthetischen Daten):

import matplotlib.pyplot as plt 

plt.figure() 
xx,yy = np.meshgrid(range(100),range(100)) 
gg = np.sqrt(xx*2+yy*2) 
CS = plt.contourf(gg) #, cc, zz_miss) 
proxy = [plt.Rectangle((0,0),1,1,fc = pc.get_facecolor()[0]) for pc in CS.collections] 

plt.legend(proxy, [str(i) for i in range(8)]) 
plt.xlabel('gamma') 
plt.ylabel('C = 1/lambda') 

plt.show() 

, das Ergebnis ist folgendes:

Manually building the color legend of contourf

1

Dank.
auch gefunden, eine etwas leichter ein: see result

CS = plt.contourf(gg, cc, zz_miss, alpha= 1) 
nm, lbl = CS.legend_elements() 
plt.legend(nm, lbl, title= 'MyTitle', fontsize= 8) 
plt.xlabel('gamma') 
plt.ylabel('C = 1/lambda') 

hier ist das Ergebnis: