Erweiterte Matplotlib-Plot-Funktionen
Matplotlib-Referenzdokumentation
Zusätzlich zu grundlegenden Zeichenfunktionen wie plot() und bar() bietet Matplotlib auch eine Vielzahl spezialisierter Zeichenfunktionen für Szenarien wie Stufendiagramme, Stammdiagramme, gestapelte Flächendiagramme und Polardiagramme.
Funktionsübersicht
| Funktion | Beschreibung |
|---|---|
| step() | Ein Stufendiagramm zeichnen (Daten ändern sich in abschnittsweise konstanten Schritten) |
| stem() | Ein Stammdiagramm zeichnen (vertikale Linien + obere Markierungen) |
| eventplot() | Ein Event-Plot zeichnen (mehrere horizontale Linien, die Ereignispositionen markieren) |
| stackplot() | Ein gestapeltes Flächendiagramm zeichnen |
| broken_barh() | Ein horizontales gebrochenes Balkendiagramm zeichnen (Gantt-Diagramm-Stil) |
| vlines() / hlines() | Mehrere vertikale/horizontale Referenzlinien zeichnen |
| fill() | Gefüllte Polygone zeichnen |
| polar() | In einem Polarkoordinatensystem zeichnen |
| loglog() / semilogx() / semilogy() | Liniendiagramm mit logarithmischen Koordinaten |
| grouped_bar() | Gruppiertes Balkendiagramm (neu in 3.10+) |
| bar_label() / pie_label() | Wertbeschriftungen zu Balken-/Kreisdiagrammen hinzufügen |
step() - Stufenplot
matplotlib.pyplot.step(x, y, *args, where='pre', **kwargs)
| Parameter | Beschreibung |
|---|---|
| x, y | Datenpunktkoordinaten |
| wobei | Stufenposition: 'pre' (davor), 'post' (danach), 'mid' (Mitte) |
Beispiele
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.5)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
# Vergleiche die drei where-Modi
ax.plot(x, y, 'gray', alpha=0.3, label='Original (plot)')
ax.step(x, y, where='pre', label='where="pre"', linewidth=2)
ax.step(x, y, where='post', label='where="post"', linewidth=2)
ax.step(x, y, where='mid', label='where="mid"', linewidth=2)
ax.set_title('step() - Treppenplot')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.arange(0, 10, 0.5)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
# Vergleiche die drei where-Modi
ax.plot(x, y, 'gray', alpha=0.3, label='Original (plot)')
ax.step(x, y, where='pre', label='where="pre"', linewidth=2)
ax.step(x, y, where='post', label='where="post"', linewidth=2)
ax.step(x, y, where='mid', label='where="mid"', linewidth=2)
ax.set_title('step() - Treppenplot')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
stem() - Stängelplot
matplotlib.pyplot.stem(*args, linefmt=None, markerfmt=None,
basefmt=None, bottom=0, orientation='vertical', **kwargs)
Beispiele
import matplotlib.pyplot as plt
import numpy as np
n = 30
x = np.arange(n)
y = np.random.randn(n) * 2
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# Vertikaler Stängelplot
ax1.stem(x, y, linefmt='steelblue', markerfmt='o',
basefmt='gray')
ax1.set_title('Vertikaler stem()')
# Horizontaler Stängelplot
ax2.stem(x, y, linefmt='coral', markerfmt='s',
basefmt='gray', orientation='horizontal')
ax2.set_title('Horizontaler stem()')
plt.show()
import numpy as np
n = 30
x = np.arange(n)
y = np.random.randn(n) * 2
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# Vertikaler Stängelplot
ax1.stem(x, y, linefmt='steelblue', markerfmt='o',
basefmt='gray')
ax1.set_title('Vertikaler stem()')
# Horizontaler Stängelplot
ax2.stem(x, y, linefmt='coral', markerfmt='s',
basefmt='gray', orientation='horizontal')
ax2.set_title('Horizontaler stem()')
plt.show()
stackplot() - Gestapeltes Flächendiagramm
matplotlib.pyplot.stackplot(x, *args, labels=None, colors=None,
baseline='zero', **kwargs)
Beispiele
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y1 = np.sin(x) + 2
y2 = np.cos(x) + 2
y3 = 0.5 * np.sin(2*x) + 2
fig, ax = plt.subplots(figsize=(8, 5), layout='constrained')
ax.stackplot(x, y1, y2, y3,
labels=['Signal 1', 'Signal 2', 'Signal 3'],
colors=['#3498db', '#e74c3c', '#2E7DCC'],
Alpha=0.7)
ax.set_title('stackplot() - Gestapeltes Flächendiagramm')
ax.legend(loc='upper right')
plt.show()
import numpy as np
x = np.arange(0, 10, 0.1)
y1 = np.sin(x) + 2
y2 = np.cos(x) + 2
y3 = 0.5 * np.sin(2*x) + 2
fig, ax = plt.subplots(figsize=(8, 5), layout='constrained')
ax.stackplot(x, y1, y2, y3,
labels=['Signal 1', 'Signal 2', 'Signal 3'],
colors=['#3498db', '#e74c3c', '#2E7DCC'],
Alpha=0.7)
ax.set_title('stackplot() - Gestapeltes Flächendiagramm')
ax.legend(loc='upper right')
plt.show()
vlines() / hlines() - Referenzlinien
matplotlib.pyplot.vlines(x, ymin, ymax, colors=None, **kwargs) matplotlib.pyplot.hlines(y, xmin, xmax, colors=None, **kwargs)
Beispiele
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.plot(x, y, 'k-', linewidth=2, alpha=0.7)
# Mehrere vertikale Linien (Markierung von Vielfachen von π)
pi_x = [np.pi, 2*np.pi, 3*np.Pi]
ax.vlines(pi_x, ymin=-1.5, ymax=1.5,
colors='red', linestyles='--', alpha=0.5,
label='π-Vielfache')
# Mehrere horizontale Linien (Markierung ±1)
ax.hlines([-1, 0, 1], xmin=0, xmax=10,
colors=['gray','gray','gray'],
linestyles=['--',':','--'], alpha=0.5)
ax.set_title('vlines() und hlines()')
ax.legend()
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.plot(x, y, 'k-', linewidth=2, alpha=0.7)
# Mehrere vertikale Linien (Markierung von Vielfachen von π)
pi_x = [np.pi, 2*np.pi, 3*np.Pi]
ax.vlines(pi_x, ymin=-1.5, ymax=1.5,
colors='red', linestyles='--', alpha=0.5,
label='π-Vielfache')
# Mehrere horizontale Linien (Markierung ±1)
ax.hlines([-1, 0, 1], xmin=0, xmax=10,
colors=['gray','gray','gray'],
linestyles=['--',':','--'], alpha=0.5)
ax.set_title('vlines() und hlines()')
ax.legend()
plt.show()
loglog() / semilogx() / semilogy() - Logarithmische Koordinaten
matplotlib.pyplot.loglog(*args, **kwargs) matplotlib.pyplot.semilogx(*args, **kwargs) matplotlib.pyplot.semilogy(*args, **kwargs)
Beispiele
import matplotlib.pyplot as plt
import numpy as np
x = np.logspace(0, 3, 50) # 10^0 bis 10^3
y1 = x**2
y2 = x**1.5
y3 = np.exp(x/50)
fig, axes = plt.subplots(1, 3, figsize=(12, 4),
layout='constrained')
# Doppelt logarithmisch
axes[0].loglog(x, y1, 'b-', label='x²')
axes[0].loglog(x, y2, 'r--', label='x¹·⁵')
axes[0].set_title('loglog()')
axes[0].grid(True, alpha=0.3)
axes[0].legend()
# Logarithmische x-Achse
axes[1].semilogx(x, y3, 'green', label='exp(x/50)')
axes[1].set_title('semilogx()')
axes[1].grid(True, alpha=0.3)
# Logarithmische y-Achse
axes[2].semilogy(x, y3, 'purple', label='exp(x/50)')
axes[2].set_title('semilogy()')
axes[2].grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.logspace(0, 3, 50) # 10^0 bis 10^3
y1 = x**2
y2 = x**1.5
y3 = np.exp(x/50)
fig, axes = plt.subplots(1, 3, figsize=(12, 4),
layout='constrained')
# Doppelt logarithmisch
axes[0].loglog(x, y1, 'b-', label='x²')
axes[0].loglog(x, y2, 'r--', label='x¹·⁵')
axes[0].set_title('loglog()')
axes[0].grid(True, alpha=0.3)
axes[0].legend()
# Logarithmische x-Achse
axes[1].semilogx(x, y3, 'green', label='exp(x/50)')
axes[1].set_title('semilogx()')
axes[1].grid(True, alpha=0.3)
# Logarithmische y-Achse
axes[2].semilogy(x, y3, 'purple', label='exp(x/50)')
axes[2].set_title('semilogy()')
axes[2].grid(True, alpha=0.3)
plt.show()
eventplot() - Ereignisdiagramm
matplotlib.pyplot.eventplot(positions, orientation='horizontal',
lineoffsets=1, linelengths=1, linewidths=None, colors=None, **kwargs)
Beispiele
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
# Ereigniszeiten für drei Kanäle
events_A = np.random.rand(20) * 10
events_B = np.random.rand(15) * 10
events_C = np.random.rand(25) * 10
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.eventplot([events_A, events_B, events_C],
colors=['#e74c3c', '#3498db', '#2E7DCC'],
lineoffsets=[0, 1, 2], # Vertikale Position jeder Linie
linelengths=0.8,
linewidths=1.5)
ax.set_title('eventplot() - Ereignis-Zeitdiagramm')
ax.set_xlabel('Zeit (s)')
ax.set_yticks([0, 1, 2])
ax.set_yticklabels(['Kanal A', 'Kanal B', 'Kanal C'])
plt.show()
import numpy as np
np.random.seed(42)
# Ereigniszeiten für drei Kanäle
events_A = np.random.rand(20) * 10
events_B = np.random.rand(15) * 10
events_C = np.random.rand(25) * 10
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.eventplot([events_A, events_B, events_C],
colors=['#e74c3c', '#3498db', '#2E7DCC'],
lineoffsets=[0, 1, 2], # Vertikale Position jeder Linie
linelengths=0.8,
linewidths=1.5)
ax.set_title('eventplot() - Ereignis-Zeitdiagramm')
ax.set_xlabel('Zeit (s)')
ax.set_yticks([0, 1, 2])
ax.set_yticklabels(['Kanal A', 'Kanal B', 'Kanal C'])
plt.show()
broken_barh() - Unterbrochenes Balkendiagramm
matplotlib.pyplot.broken_barh(xranges, yrange, **kwargs)
Beispiele
import matplotlib.pyplot as plt
# Gantt-Diagramm simulieren
tasks = [
[(0, 5)], # Aufgabe 1: beginnt bei 0, dauert 5
[(3, 4)], # Aufgabe 2: beginnt bei 3, dauert 4
[(2, 3), (6, 3)], # Aufgabe 3: zwei Zeiträume
[(5, 4)], # Aufgabe 4
[(1, 2), (4, 2), (8, 2)], # Aufgabe 5: drei Zeiträume
]
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
colors = ['#3498db', '#e74c3c', '#2E7DCC', '#f39c12', '#9b59b6']
for i, (task, color) in enumerate(zip(tasks, colors)):
ax.broken_barh(task, (i - 0.4, 0.8), facecolors=color)
ax.set_title('broken_barh() - Gantt-ähnliches Diagramm')
ax.set_xlabel('Zeit')
ax.set_yticks(range(5))
ax.set_yticklabels([f'Aufgabe {i+1}' for i in range(5)])
ax.grid(True, alpha=0.3)
plt.show()
# Gantt-Diagramm simulieren
tasks = [
[(0, 5)], # Aufgabe 1: beginnt bei 0, dauert 5
[(3, 4)], # Aufgabe 2: beginnt bei 3, dauert 4
[(2, 3), (6, 3)], # Aufgabe 3: zwei Zeiträume
[(5, 4)], # Aufgabe 4
[(1, 2), (4, 2), (8, 2)], # Aufgabe 5: drei Zeiträume
]
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
colors = ['#3498db', '#e74c3c', '#2E7DCC', '#f39c12', '#9b59b6']
for i, (task, color) in enumerate(zip(tasks, colors)):
ax.broken_barh(task, (i - 0.4, 0.8), facecolors=color)
ax.set_title('broken_barh() - Gantt-ähnliches Diagramm')
ax.set_xlabel('Zeit')
ax.set_yticks(range(5))
ax.set_yticklabels([f'Aufgabe {i+1}' for i in range(5)])
ax.grid(True, alpha=0.3)
plt.show()
fill() - Gefülltes Polygon
matplotlib.pyplot.fill(*args, **kwargs)
Beispiele
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(layout='constrained')
# Eine Raute füllen
x = [1, 2, 1, 0]
y = [0, 1, 2, 1]
ax.fill(x, y, alpha=0.5, color='steelblue',
edgecolor='black', label='Raute')
# Ein Fünfeck füllen
theta = np.linspace(0, 2*np.pi, 6)
x2 = 2.5 + np.cos(theta)
y2 = 1 + np.sin(theta)
ax.fill(x2, y2, alpha=0.5, color='coral',
edgecolor='black', label='Pentagon')
ax.set_title('fill() - Gefüllte Polygone')
ax.set_aspect('equal')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
fig, ax = plt.subplots(layout='constrained')
# Eine Raute füllen
x = [1, 2, 1, 0]
y = [0, 1, 2, 1]
ax.fill(x, y, alpha=0.5, color='steelblue',
edgecolor='black', label='Raute')
# Ein Fünfeck füllen
theta = np.linspace(0, 2*np.pi, 6)
x2 = 2.5 + np.cos(theta)
y2 = 1 + np.sin(theta)
ax.fill(x2, y2, alpha=0.5, color='coral',
edgecolor='black', label='Pentagon')
ax.set_title('fill() - Gefüllte Polygone')
ax.set_aspect('equal')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
Andere Erweiterungen