Wie man die Größe oder den Abstand von Subplots mit vielen Subplots in Matplotlib verbessert
-
Methode
tight_layout() -
Methode
plt.subplots_adjust() -
Verfahren
plt.subplot_tool() -
Aktivieren Sie die Funktion
constrained_layout=Truein Subplots
Wir könnten die Methoden tight_layout(), subplots_adjust() und subplot_tool() verwenden, um die Größe oder den Abstand von Subplots mit vielen Subplots in der Matplotlib zu verbessern. Wir können auch die Abstände zwischen den Subplots verbessern, indem wir constrained_layout=True in der subplots() Funktion setzen.
Methode tight_layout()
Die Methode tight_layout() hält automatisch die korrekten Abstände zwischen den Teilplots ein.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = 1 / (1 + np.exp(-x))
y4 = np.exp(x)
fig, ax = plt.subplots(2, 2)
ax[0, 0].plot(x, y1)
ax[0, 1].plot(x, y2)
ax[1, 0].plot(x, y3)
ax[1, 1].plot(x, y4)
ax[0, 0].set_title("Sine function")
ax[0, 1].set_title("Cosine function")
ax[1, 0].set_title("Sigmoid function")
ax[1, 1].set_title("Exponential function")
fig.tight_layout()
plt.show()

Wenn wir die tight_layout() Methode nicht verwenden, wird sich eine Zeile mit dem Titel der nächsten überschneiden.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = 1 / (1 + np.exp(-x))
y4 = np.exp(x)
fig, ax = plt.subplots(2, 2)
ax[0, 0].plot(x, y1)
ax[0, 1].plot(x, y2)
ax[1, 0].plot(x, y3)
ax[1, 1].plot(x, y4)
ax[0, 0].set_title("Sine function")
ax[0, 1].set_title("Cosine function")
ax[1, 0].set_title("Sigmoid function")
ax[1, 1].set_title("Exponential function")
plt.show()

Methode plt.subplots_adjust()
Wir können die Methode plt.subplots_adjust() verwenden, um den Abstand zwischen den Subplots zu ändern.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = 1 / (1 + np.exp(-x))
y4 = np.exp(x)
fig, ax = plt.subplots(2, 2)
ax[0, 0].plot(x, y1)
ax[0, 1].plot(x, y2)
ax[1, 0].plot(x, y3)
ax[1, 1].plot(x, y4)
ax[0, 0].set_title("Sine function")
ax[0, 1].set_title("Cosine function")
ax[1, 0].set_title("Sigmoid function")
ax[1, 1].set_title("Exponential function")
plt.subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=0.35)
plt.show()

plt.subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=0.35)
wspace und hspace geben den Platz an, der zwischen den Teilflächen reserviert ist. Sie sind die Bruchteile der Achsenbreite bzw. -höhe.
Die Parameter Links, Rechts, Oben und Unten geben die Positionen von vier Seiten der Nebengrafiken an. Sie sind die Bruchteile der Breite und Höhe der Abbildung.
Verfahren plt.subplot_tool()
Diese Methode startet ein Subplot-Werkzeugfenster für eine Figur.
import numpy as np
import matplotlib.pyplot as plt
im1 = np.random.random((50, 50))
im2 = np.random.random((40, 50))
im3 = np.random.random((50, 40))
im4 = np.random.random((60, 50))
plt.subplot(221)
plt.imshow(im1)
plt.subplot(222)
plt.imshow(im2)
plt.subplot(223)
plt.imshow(im3)
plt.subplot(224)
plt.imshow(im4)
plt.subplot_tool()
plt.show()

Es bietet eine interaktive Methode für den Benutzer, den Balken im subplot_tool zu ziehen, um das Layout der Subplots zu ändern.
Aktivieren Sie die Funktion constrained_layout=True in Subplots
constrained_layout passt Subplots und Dekorationen automatisch so an, dass sie so gut wie möglich in die Abbildung passen.
constrained_layout muss vor oder während der Erstellung von Subplots aktiviert werden, da es das Layout vor jedem Zeichenschritt optimiert.
import numpy as np
import matplotlib.pyplot as plt
a = np.linspace(0, 5, 100)
figure, axes = plt.subplots(2, 2, constrained_layout=True)
axes[0, 0].plot(x, np.exp(a))
axes[0, 1].plot(a, np.sin(a))
axes[1, 0].plot(a, np.cos(a))
axes[1, 1].plot(range(10))
axes[0, 0].set_title("subplot 1")
axes[0, 1].set_title("subplot 2")
axes[1, 0].set_title("subplot 3")
axes[1, 1].set_title("subplot 4")
plt.show()

Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn