Legenden in Subplots

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
schlake042
User
Beiträge: 7
Registriert: Dienstag 19. Februar 2019, 18:30

Hey,

ich schaffe es nicht in einem Subplot eine Legende einzufügen.

Code: Alles auswählen

plt.figure(figsize=(30, 10))
plt.subplot(221)
plt.gca().set_title('Average Goals per Stage per Game' )
plt.plot(average_goals_per_stage)
plt.subplot(222)
plt.gca().set_title('Average Goal Difference Home vs Out per Stage')
plt.plot(average_difference)
average_goals_per_stage und average_difference sind jeweils Dataframes. In den columns stehen 4 Länder, die ich gerne in der Legende haben will.


So soll es aussehen : Bild

So sieht es aus :Bild

Der DataFrame sieht so aus: Bild

Habe schon viele Sachen probiert, kriege es aber nicht hin. Habt ihr einen Tipp ? Vielen Dank !

Viele Grüße
Benutzeravatar
__blackjack__
User
Beiträge: 14336
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@schlake042: Welche Zeile in dem gezeigten Code sollte denn für die Legende zuständig sein? Was hast Du denn probiert?
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
schlake042
User
Beiträge: 7
Registriert: Dienstag 19. Februar 2019, 18:30

In dem gezeigten Code im Moment keine. Wenn ich das Diagramm ohne subplot "plotte", also nur average_goals_per_stage.plot(), dann ist die Legende auch da.

Ich habe schon vieles wild durchprobiert. Ich verstehe nur nicht, wie ich die Column-Überschriften in die Legende bekomme.

z.B.

Code: Alles auswählen

plt.figure(figsize=(30, 10))
            ax = plt.subplot(221)
            ax.plot(average_goals_per_stage)
            ax.legend()
            plt.gca().set_title('Average Goals per Stage per Game' + ' ' + season)
Fehlermeldung : "No handles with labels found to put in legend."

Ich google auch schon lange rum, aber mit dieser Variante finde ich einfach nichts anwendbares.
Benutzeravatar
__blackjack__
User
Beiträge: 14336
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@schlake042: Na dann schau Dir doch mal an was für Argumente `legend()` so nimmt.
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
Antworten