Als erstes: IPython interaktiv machen:
%matplotlib inline
# bei euch: %matplotlib (nur in iPython)
Um mit Matplotlib arbeiten zu können, muss die Bibliothek erst einmal importiert werden. Damit wir nicht so viel tippen müssen geben wir ihr einen kürzeren Namen:
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (10, 8)
plt.rcParams['font.size'] = 16
plt.rcParams['lines.linewidth'] = 2
Außerdem brauchen wir ein paar Funktion aus numpy
, die euch schon bekannt sind
import numpy as np
Ein einfaches Beispiel: $f(x)=x^2$
x = np.linspace(0, 1) # gibt 50 Zahlen in gleichmäßigem Abstand von 0–1
plt.plot(x, x**2)
# Falls nicht interaktiv:
# plt.show()
[<matplotlib.lines.Line2D at 0x7f2ab0751670>]
Anderes Beispiel: $\sin(t)$ mit verschiedenen Stilen. Vorsicht, die Funktionen und $\pi$ sind Bestandteil von numpy
t = np.linspace(0, 2 * np.pi)
plt.plot(t, np.sin(t))
[<matplotlib.lines.Line2D at 0x7f2aa0f1cf70>]
plt.plot(t, np.sin(t), 'r--')
[<matplotlib.lines.Line2D at 0x7f2aa0ea1310>]
plt.plot(t, np.sin(t), 'go')
[<matplotlib.lines.Line2D at 0x7f2aa0e84fa0>]
Tabelle mit allen Farben und Styles: matplotlib.axes.Axes.plot
Neue Grenzen mit xlim(a, b)
und ylim(a, b)
plt.plot(t, np.sin(t))
plt.xlim(0, 2 * np.pi)
plt.ylim(-1.2, 1.2)
(-1.2, 1.2)
with plt.xkcd():
plt.title('Axes with labels')
plt.plot(t, np.sin(t))
plt.xlabel('t / s')
plt.ylabel('U / V')
plt.ylim(-1.1, 1.1)
plt.xlim(0, 2 * np.pi)