import matplotlib.pyplot as plt
import numpy as np
= np.linspace(0, 10, 100)
x = np.sin(x)
y
plt.plot(x, y) plt.show()
12 Best Practices in Matplotlib: Fehler und Verbesserungen
In diesem Kapitel zeigen wir für häufige Problemstellungen jeweils ein schlechtes und ein verbessertes Beispiel.
12.1 1. Fehlende Beschriftungen
12.1.1 ❌ Schlechtes Beispiel
12.1.2 ✅ Besseres Beispiel
='sin(x)', color='b')
plt.plot(x, y, label'Zeit (s)')
plt.xlabel('Amplitude')
plt.ylabel('Sinuskurve')
plt.title(
plt.legend() plt.show()
12.2 2. Ungünstige Farbwahl
12.2.1 ❌ Schlechtes Beispiel
='yellow')
plt.plot(x, y, color plt.show()
12.2.2 ✅ Besseres Beispiel
='darkblue')
plt.plot(x, y, colorTrue, linestyle='--', alpha=0.7)
plt.grid('Gute Kontraste für bessere Lesbarkeit')
plt.title( plt.show()
12.3 3. Keine sinnvolle Achsenskalierung
12.3.1 ❌ Schlechtes Beispiel
plt.plot(x, y)0.5, 1)
plt.ylim( plt.show()
12.3.2 ✅ Besseres Beispiel
plt.plot(x, y)-1.2, 1.2)
plt.ylim(0, 10)
plt.xlim(True)
plt.grid('Sinnvolle Achsenskalierung')
plt.title( plt.show()
12.4 4. Überladung durch zu viele Linien
12.4.1 ❌ Schlechtes Beispiel
for i in range(10):
+ i * 0.2))
plt.plot(x, np.sin(x plt.show()
12.4.2 ✅ Besseres Beispiel
='sin(x)')
plt.plot(x, np.sin(x), label='cos(x)')
plt.plot(x, np.cos(x), label
plt.legend()'Weniger ist mehr: Reduzierte Informationsdichte')
plt.title(True)
plt.grid( plt.show()
12.5 Fazit
Gute Plots zeichnen sich durch klare Beschriftungen, gute Lesbarkeit und eine sinnvolle Informationsdichte aus.