Matplotlib-Funktionen für Span-Linien und Vektorfelder
Matplotlib-Referenzdokumentation
Span-Linien-Funktionen werden verwendet, um Referenzlinien und schattierte Bänder hinzuzufügen, die die gesamten Axes in einem Diagramm überspannen; Vektorfeld-Funktionen werden verwendet, um Pfeildiagramme und Stromlinien-Diagramme zu zeichnen.
Funktionsübersicht
| Funktion | Beschreibung |
|---|---|
| axhline() | Eine horizontale Linie hinzufügen, die die Axes überspannt |
| axhspan() | Ein horizontales schattiertes Band hinzufügen, das die Axes überspannt |
| axvline() | Eine vertikale Linie hinzufügen, die die Axes überspannt |
| axvspan() | Ein vertikales schattiertes Band hinzufügen, das die Axes überspannt |
| axline() | Fügt eine unendliche Linie hinzu, die durch zwei Punkte verläuft |
| quiver() | Ein Vektorfeld zeichnen (Pfeildiagramm) |
| quiverkey() | Eine Skalenlegende zu einem Vektorfeld hinzufügen |
| barbs() | Ein Windbarben-Diagramm zeichnen (für die Meteorologie) |
| streamplot() | Stromlinien-Plot zeichnen |
Span-Funktionen
axhline() / axvline()
matplotlib.pyplot.axhline(y=0, xmin=0, xmax=1, **kwargs) matplotlib.pyplot.axvline(x=0, ymin=0, ymax=1, **kwargs)
axhspan() / axvspan()
matplotlib.pyplot.axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs) matplotlib.pyplot.axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs)
axline()
matplotlib.pyplot.axline(xy1, xy2=None, *, slope=None, **kwargs)
Beispiele
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5),
layout='constrained')
# Linker Plot: Span-Kurven
ax1.plot(x, y, 'k-', alpha=0.7)
ax1.axhline(y=0, color='gray', linewidth=0.8) # y=0 horizontale Linie
ax1.axhline(y=0.5, color='green', linestyle='--') # y=0.5
ax1.axvline(x=np.pi, color='red', linestyle='--') # x=π
ax1.axvspan(np.pi, 2*np.pi, alpha=0.15, # Schattiertes Band
color='red', label='Phase 2')
ax1.axhspan(0.5, 1.0, xmin=0.6, xmax=1.0, # Lokaler schattierter Bereich
alpha=0.15, color='blue')
ax1.set_title('Bereichsfunktionen')
ax1.legend()
# Rechtes Diagramm: axline definiert eine unendliche Gerade durch zwei Punkte
ax2.plot([0, 2, 4], [0, 2, 3], 'ro', label='Datenpunkte')
ax2.axline((0, 0), (2, 2), color='blue', linestyle='--',
label='Durch (0,0) und (2,2)')
ax2.axline((0, 0), slope=1.5, color='green',
linestyle=':', label='Von (0,0) mit Steigung=1.5')
ax2.set_xlim(-1, 5)
ax2.set_ylim(-1, 5)
ax2.set_title('axline()')
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5),
layout='constrained')
# Linker Plot: Span-Kurven
ax1.plot(x, y, 'k-', alpha=0.7)
ax1.axhline(y=0, color='gray', linewidth=0.8) # y=0 horizontale Linie
ax1.axhline(y=0.5, color='green', linestyle='--') # y=0.5
ax1.axvline(x=np.pi, color='red', linestyle='--') # x=π
ax1.axvspan(np.pi, 2*np.pi, alpha=0.15, # Schattiertes Band
color='red', label='Phase 2')
ax1.axhspan(0.5, 1.0, xmin=0.6, xmax=1.0, # Lokaler schattierter Bereich
alpha=0.15, color='blue')
ax1.set_title('Bereichsfunktionen')
ax1.legend()
# Rechtes Diagramm: axline definiert eine unendliche Gerade durch zwei Punkte
ax2.plot([0, 2, 4], [0, 2, 3], 'ro', label='Datenpunkte')
ax2.axline((0, 0), (2, 2), color='blue', linestyle='--',
label='Durch (0,0) und (2,2)')
ax2.axline((0, 0), slope=1.5, color='green',
linestyle=':', label='Von (0,0) mit Steigung=1.5')
ax2.set_xlim(-1, 5)
ax2.set_ylim(-1, 5)
ax2.set_title('axline()')
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.show()
Vektorfeld-Funktionen
quiver() - Vektorfeld
matplotlib.pyplot.quiver(*args, **kwargs) # X, Y, U, V: 网格坐标和矢量分量 # C: 可选的颜色数据
streamplot() - Stromlinien-Diagramm
matplotlib.pyplot.streamplot(x, y, u, v, density=1,
linewidth=None, color=None, cmap=None, **kwargs)
barbs() - Windbarben-Diagramm
matplotlib.pyplot.barbs(*args, **kwargs) # X, Y, U, V: 网格坐标和风分量
Beispiele
import matplotlib.pyplot as plt
import numpy as np
# Vektorfelddaten erstellen
x = np.linspace(-3, 3, 15)
y = np.linspace(-3, 3, 15)
X, Y = np.meshgrid(x, y)
# Wirbelfeld
U = -Y
V = X
# Geschwindigkeitsbetrag
speed = np.sqrt(U**2 + V**2)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5),
layout='constrained')
# Linker Plot: quiver-Pfeilplot
q = ax1.quiver(X, Y, U, V, speed, cmap='viridis',
scale=50, width=0.005)
fig.colorbar(q, ax=ax1, label='Geschwindigkeit')
ax1.set_title('quiver() - Pfeilfeld')
ax1.set_aspect('equal')
# Rechter Plot: streamplot-Stromlinienplot
strm = ax2.streamplot(x, y, U, V, color=speed,
cmap='plasma', density=1.5, linewidth=1.5)
fig.colorbar(strm.lines, ax=ax2, label='Geschwindigkeit')
ax2.set_title('streamplot() - Stromlinien')
ax2.set_aspect('equal')
plt.show()
import numpy as np
# Vektorfelddaten erstellen
x = np.linspace(-3, 3, 15)
y = np.linspace(-3, 3, 15)
X, Y = np.meshgrid(x, y)
# Wirbelfeld
U = -Y
V = X
# Geschwindigkeitsbetrag
speed = np.sqrt(U**2 + V**2)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5),
layout='constrained')
# Linker Plot: quiver-Pfeilplot
q = ax1.quiver(X, Y, U, V, speed, cmap='viridis',
scale=50, width=0.005)
fig.colorbar(q, ax=ax1, label='Geschwindigkeit')
ax1.set_title('quiver() - Pfeilfeld')
ax1.set_aspect('equal')
# Rechter Plot: streamplot-Stromlinienplot
strm = ax2.streamplot(x, y, U, V, color=speed,
cmap='plasma', density=1.5, linewidth=1.5)
fig.colorbar(strm.lines, ax=ax2, label='Geschwindigkeit')
ax2.set_title('streamplot() - Stromlinien')
ax2.set_aspect('equal')
plt.show()
barbs() Beispiel
Beispiele
import matplotlib.pyplot as plt
import numpy as np
# Simulierte Windfelddaten
x = np.linspace(0, 10, 10)
y = np.linspace(0, 8, 8)
X, Y = np.meshgrid(x, y)
# Windkomponenten (gleichmäßiger Ostwind + zufällige Störung)
U = 10 + np.random.randn(*X.shape) * 3
V = np.random.randn(*X.shape) * 5
fig, ax = plt.subplots(figsize=(8, 5), layout='constrained')
# Windbarben-Diagramm: Windbarben repräsentieren die Windgeschwindigkeit, die Richtung gibt die Windrichtung an
ax.barbs(X, Y, U, V, length=6)
ax.set_title('barbs() - Windbarben (meteorologisch)')
ax.set_xlabel('Längengrad')
ax.set_ylabel('Breitengrad')
ax.set_xlim(-1, 11)
ax.set_ylim(-1, 9)
plt.show()
import numpy as np
# Simulierte Windfelddaten
x = np.linspace(0, 10, 10)
y = np.linspace(0, 8, 8)
X, Y = np.meshgrid(x, y)
# Windkomponenten (gleichmäßiger Ostwind + zufällige Störung)
U = 10 + np.random.randn(*X.shape) * 3
V = np.random.randn(*X.shape) * 5
fig, ax = plt.subplots(figsize=(8, 5), layout='constrained')
# Windbarben-Diagramm: Windbarben repräsentieren die Windgeschwindigkeit, die Richtung gibt die Windrichtung an
ax.barbs(X, Y, U, V, length=6)
ax.set_title('barbs() - Windbarben (meteorologisch)')
ax.set_xlabel('Längengrad')
ax.set_ylabel('Breitengrad')
ax.set_xlim(-1, 11)
ax.set_ylim(-1, 9)
plt.show()
Andere Erweiterungen