Matplotlib Text- und Annotationsfunktionen auf Figure-Ebene
Matplotlib-Referenzdokumentation
Zusätzlich zu text() und annotate() bietet Matplotlib auch Textfunktionen auf Figure-Ebene, das Hinzufügen von Tabellen und das Zeichnen von Pfeilen.
Funktionsübersicht
| Funktion | Beschreibung |
|---|---|
| figtext() | Text auf Figure-Ebene hinzufügen (nicht Axes) |
| figlegend() | Legende auf Figure-Ebene hinzufügen (Axes-übergreifend) |
| table() | Eine Tabelle zu Axes hinzufügen |
| arrow() | Einen einfachen Pfeil hinzufügen |
figtext() - Text auf Figure-Ebene
matplotlib.pyplot.figtext(x, y, s, fontdict=None, **kwargs)
Ähnlich wie text(), aber das Koordinatensystem bezieht sich auf Figure-Koordinaten (0-1), nicht auf Datenkoordinaten.
Beispiel
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot([0, 1], [0, 1], 'steelblue')
ax.set_title('Axes-Ebene (Text vs. figtext)')
# Text auf Axes-Ebene (Datenkoordinaten)
ax.text(0.3, 0.7, 'Axes-Text (Datenkoordinaten)',
fontsize=12, color='blue')
# Text auf Figure-Ebene (relative Figure-Koordinaten 0-1)
plt.figtext(0.5, 0.02,
'Figure-Text unten mittig (Figure-Koordinaten)',
ha='center', fontsize=10, color='gray')
plt.figtext(0.02, 0.95,
'Oben links',
ha='left', va='top',
fontsize=12, fontweight='bold', color='red')
plt.show()
import numpy as np
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot([0, 1], [0, 1], 'steelblue')
ax.set_title('Axes-Ebene (Text vs. figtext)')
# Text auf Axes-Ebene (Datenkoordinaten)
ax.text(0.3, 0.7, 'Axes-Text (Datenkoordinaten)',
fontsize=12, color='blue')
# Text auf Figure-Ebene (relative Figure-Koordinaten 0-1)
plt.figtext(0.5, 0.02,
'Figure-Text unten mittig (Figure-Koordinaten)',
ha='center', fontsize=10, color='gray')
plt.figtext(0.02, 0.95,
'Oben links',
ha='left', va='top',
fontsize=12, fontweight='bold', color='red')
plt.show()
figlegend() - Legende auf Figure-Ebene
matplotlib.pyplot.figlegend(*args, **kwargs)
Wenn mehrere Axes eine Legende gemeinsam nutzen müssen, kann figlegend() die Legende auf Figure-Ebene platzieren.
Beispiel
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# Zeichne dieselben zwei Kurven auf jedem Axes, aber füge keine separaten Legenden hinzu
ax1.plot(x, np.sin(x), 'b-', label='sin(x)')
ax1.plot(x, np.cos(x), 'r--', label='cos(x)')
ax1.set_title('Diagramm 1')
ax2.plot(x, np.sin(x), 'b-', label='sin(x)')
ax2.plot(x, np.cos(x), 'r--', label='cos(x)')
ax2.set_title('Diagramm 2')
# Legende auf Figure-Ebene (oben außerhalb der Figure platziert)
fig.legend(loc='outside upper center', ncol=2,
fontsize=11, frameon=True)
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# Zeichne dieselben zwei Kurven auf jedem Axes, aber füge keine separaten Legenden hinzu
ax1.plot(x, np.sin(x), 'b-', label='sin(x)')
ax1.plot(x, np.cos(x), 'r--', label='cos(x)')
ax1.set_title('Diagramm 1')
ax2.plot(x, np.sin(x), 'b-', label='sin(x)')
ax2.plot(x, np.cos(x), 'r--', label='cos(x)')
ax2.set_title('Diagramm 2')
# Legende auf Figure-Ebene (oben außerhalb der Figure platziert)
fig.legend(loc='outside upper center', ncol=2,
fontsize=11, frameon=True)
plt.show()
table() - Tabelle zu Axes hinzufügen
matplotlib.pyplot.table(cellText=None, cellColours=None,
cellLoc='right', colWidths=None, rowLabels=None,
rowColours=None, rowLoc='left', colLabels=None,
colColours=None, colLoc='center', loc='bottom',
bbox=None, edges='closed', **kwargs)
Beispiel
import matplotlib.pyplot as plt
import numpy as np
# Anzuzeigende Daten
data = [[85, 90, 78, 92],
[76, 88, 82, 85],
[90, 93, 88, 95]]
row_labels = ['Schüler A', 'Schüler B', 'Schüler C']
col_labels = ['Mathe', 'Englisch', 'Naturwissenschaften', 'Geschichte']
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
# Vergleichsdiagramm zeichnen
x = np.arange(len(col_labels))
for i, (row, label) in enumerate(zip(data, row_labels)):
ax.plot(x, row, 'o-', label=label, markersize=8)
ax.set_xticks(x)
ax.set_xticklabels(col_labels)
ax.set_title('Schülerergebnisse')
ax.legend()
ax.grid(True, alpha=0.3)
# Tabelle unter dem Diagramm hinzufügen
table = ax.table(cellText=data,
rowLabels=row_labels,
colLabels=col_labels,
cellLoc='center',
loc='bottom',
bbox=[0, -0.35, 1, 0.25]) # [left, bottom, width, height]
plt.show()
import numpy as np
# Anzuzeigende Daten
data = [[85, 90, 78, 92],
[76, 88, 82, 85],
[90, 93, 88, 95]]
row_labels = ['Schüler A', 'Schüler B', 'Schüler C']
col_labels = ['Mathe', 'Englisch', 'Naturwissenschaften', 'Geschichte']
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
# Vergleichsdiagramm zeichnen
x = np.arange(len(col_labels))
for i, (row, label) in enumerate(zip(data, row_labels)):
ax.plot(x, row, 'o-', label=label, markersize=8)
ax.set_xticks(x)
ax.set_xticklabels(col_labels)
ax.set_title('Schülerergebnisse')
ax.legend()
ax.grid(True, alpha=0.3)
# Tabelle unter dem Diagramm hinzufügen
table = ax.table(cellText=data,
rowLabels=row_labels,
colLabels=col_labels,
cellLoc='center',
loc='bottom',
bbox=[0, -0.35, 1, 0.25]) # [left, bottom, width, height]
plt.show()
arrow() - Pfeil hinzufügen
matplotlib.pyplot.arrow(x, y, dx, dy, **kwargs)
arrow() ist eine Kurzform zum Hinzufügen einfacher Pfeile. Für komplexe Pfeilannotationen wird annotate() empfohlen.
Beispiel
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(7, 5), layout='constrained')
# Koordinatensystem zeichnen
ax.spines[['left', 'bottom']].set_position(('data', 0))
ax.spines[['top', 'right']].set_visible(False)
ax.set_xlim(-1, 5)
ax.set_ylim(-1, 5)
# Verschiedene Pfeile
ax.arrow(0, 0, 3, 0, head_width=0.2, head_length=0.2,
fc='red', ec='red', label='3 Einheiten nach rechts')
ax.arrow(0, 0, 0, 3, head_width=0.2, head_length=0.2,
fc='blue', ec='blue', label='3 Einheiten nach oben')
ax.arrow(0, 0, 2, 2, head_width=0.2, head_length=0.2,
fc='green', ec='green', label='Diagonal')
ax.set_title('arrow() - Einfache Pfeile')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
fig, ax = plt.subplots(figsize=(7, 5), layout='constrained')
# Koordinatensystem zeichnen
ax.spines[['left', 'bottom']].set_position(('data', 0))
ax.spines[['top', 'right']].set_visible(False)
ax.set_xlim(-1, 5)
ax.set_ylim(-1, 5)
# Verschiedene Pfeile
ax.arrow(0, 0, 3, 0, head_width=0.2, head_length=0.2,
fc='red', ec='red', label='3 Einheiten nach rechts')
ax.arrow(0, 0, 0, 3, head_width=0.2, head_length=0.2,
fc='blue', ec='blue', label='3 Einheiten nach oben')
ax.arrow(0, 0, 2, 2, head_width=0.2, head_length=0.2,
fc='green', ec='green', label='Diagonal')
ax.set_title('arrow() - Einfache Pfeile')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
Andere Erweiterungen