Hinweis
Zum Ende springen, um den vollständigen Beispielcode herunterzuladen.
Mehrere Subplots mit plt.subplots erstellen#
pyplot.subplots erstellt mit einem einzigen Aufruf eine Figure und ein Gitter von Subplots und bietet dabei eine angemessene Kontrolle darüber, wie die einzelnen Plots erstellt werden. Für fortgeschrittenere Anwendungsfälle können Sie GridSpec für ein allgemeineres Subplot-Layout oder Figure.add_subplot zum Hinzufügen von Subplots an beliebigen Stellen innerhalb der Figure verwenden.
Eine Figure mit nur einem Subplot#
subplots() gibt ohne Argumente eine Figure und eine einzelne Axes zurück.
Dies ist tatsächlich der einfachste und empfohlene Weg, eine einzelne Figure und Axes zu erstellen.
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('A single plot')

Subplots in einer Richtung stapeln#
Die ersten beiden optionalen Argumente von pyplot.subplots definieren die Anzahl der Zeilen und Spalten des Subplot-Gitters.
Beim Stapeln nur in einer Richtung ist die zurückgegebene axs ein 1D-NumPy-Array, das die Liste der erstellten Axes enthält.
fig, axs = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
axs[0].plot(x, y)
axs[1].plot(x, -y)

Wenn Sie nur wenige Axes erstellen, ist es praktisch, diese sofort in dedizierte Variablen für jede Axes zu entpacken. Auf diese Weise können wir ax1 anstelle des umständlicheren axs[0] verwenden.
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)

Um nebeneinander liegende Subplots zu erhalten, übergeben Sie die Parameter 1, 2 für eine Zeile und zwei Spalten.
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)

Subplots in zwei Richtungen stapeln#
Beim Stapeln in zwei Richtungen ist die zurückgegebene axs ein 2D-NumPy-Array.
Wenn Sie Parameter für jeden Subplot festlegen müssen, ist es praktisch, alle Subplots in einem 2D-Gitter mit for ax in axs.flat: zu durchlaufen.
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')
for ax in axs.flat:
ax.set(xlabel='x-label', ylabel='y-label')
# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
ax.label_outer()
![Axis [0, 0], Axis [0, 1], Axis [1, 0], Axis [1, 1]](../../_images/sphx_glr_subplots_demo_005.png)
Sie können auch in 2D Tupel-Entpackung verwenden, um alle Subplots dedizierten Variablen zuzuweisen
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')
for ax in fig.get_axes():
ax.label_outer()

Polar-Axes#
Der Parameter subplot_kw von pyplot.subplots steuert die Eigenschaften des Subplots (siehe auch Figure.add_subplot). Insbesondere kann dieser verwendet werden, um ein Gitter von Polar-Axes zu erstellen.
Gesamtlaufzeit des Skripts: (0 Minuten 7,941 Sekunden)





