2016-05-26 8 views
2

ich die Antworten gelesen habe bereits im Zusammenhang mit diesem und habe dies gefolgt:Wie Plots in Abbildung aggregieren mit Nebenhandlungen

fig = plt.figure() 
fig.add_subplot(221) #top left 
fig.add_subplot(222) #top right 
fig.add_subplot(223) #bottom left 
fig.add_subplot(224) #bottom right 
plt.show() 

Das ist sehr gut, jetzt habe ich eine Figur mit vier leeren Nebenhandlungen.

Was ich nicht herausfinden kann, ist, wie die Unterplot zu zeigen, mein Grundstück x wo x ist ein matplotlib.axes.AxesSubplot, dass ich bereits generiert habe.

insgesamt Mein Code ist:

plt1 = plot.scatter(foo,bar) 
plt2 = plot.line(bar, baz) 
plt3 = plot.scatter(you, get) 
plt4 = plot.line(the, picture) 

fig = plt.figure() 
fig.add_subplot(221) #top left 
fig.add_subplot(222) #top right 
fig.add_subplot(223) #bottom left 
fig.add_subplot(224) #bottom right 

# Here is where I got lost 
# I want figure subplot 221 to show plt1 etc... 

Antwort

4

Sie müssen halten die Achsen Spur von add_subplot zurückgegebenen Objekte.

fig = plt.figure() 
ax1 = fig.add_subplot(221) #top left 
ax2 = fig.add_subplot(222) #top right 
ax3 = fig.add_subplot(223) #bottom left 
ax4 = fig.add_subplot(224) #bottom right 

Und dann können Sie tun:

ax1.plot(...) 
ax2.boxplot(...) 
ax3.hist(...) 
# etc 

Aber ich würde dies nur tun:

fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=2, ncols=2) 

und gehen von dort aus.

+0

Ich denke, was mich aufhält, ist, dass 'ax1.plot (...)' eine Menge der Formatierungen entfernt, die ich mit 'erreichen konnte df [['col1', 'col2']]]. plot() ', dh Anzeige des Index der Datumsstempel als x-Achse anstelle von Index n und Achsentiteln usw. Das Diagramm, das ich mag, wird generiert von 'statusPlt = df [['a', 'b', 'c']]. plot (title = 'Status von stuff by Create Date')' was mir ein schönes Liniendiagramm zeigt, gruppiert nach Spaltenwert über dem Datumsindex Ersetzen Sie es mit 'ax1.plot (df [['a', 'b', 'c']])' zeigt die x-Achse als Indexwert –

+1

, also sollten Sie 'df [['col1' tun , 'col2']]. plot (ax = ax1) ' –

+1

@TylerWood und setzen Sie die Formatierung direkt auf die Achsen (zB' ax.set_xlabel (...) ') –

1

Ist es hauptsächlich vor dem Erstellen Nebenhandlungen zu ziehen? Wenn nicht, versuchen Sie diesen Code:

fig = plt.figure() 
fig.add_subplot(221) #top left 
plt1 = plt.scatter(foo,bar) 
fig.add_subplot(222) #top right 
plt2 = plt.line(bar, baz) 
fig.add_subplot(223) #bottom left 
plt3 = plt.scatter(you, get) 
fig.add_subplot(224) #bottom right 
plt4 = plt.line(the, picture) 
+0

Nur weil ich bereits den Code für die Plots eingerichtet habe, wie ich will, dass sie erscheinen. Wenn Sie diese Route fahren, müssen Sie erneut die Achsenbezeichnungen, Titel, Legenden, Achsenskalen usw. einstellen. –

+0

Ja, ich verstehe Sie. Sieh dir diese Antwort an http://StackOverflow.com/a/16754215 – Serenity