import numpy as np
np.pi
A1 = np.array([1,2,3])
A1
Ein eindimensionaler array
ist wie ein Vektor
3*A1
A1**2
Ein array
braucht aber nicht eindimensional zu sein
A2 = np.array([[1,2,3], [4,5,6]])
A2
A2**2
A1
A1[1]
A2[1:, :]
A2[1:, :].shape
A2.flatten().reshape(2,3)
A2[1:, :].flatten()
A2.reshape(3,2)
np.arange(10)
x = np.linspace(0, 5*np.pi)
x
A3 = np.arange(1,5).reshape(2,2)
M2 = A3 @ A2
M2
A1
A2 @ A1
np.sin(x)
%%timeit
y = np.sin(x)
%%timeit
l = []
for xx in x:
l.append(np.sin(xx))
y = np.array(l)
import matplotlib.pyplot as plt
%matplotlib inline
y = np.sin(x)
plt.plot(x, y) ;
Das ist zu zackelig und die Striche sind zu dünn
x = np.linspace(0, 5*np.pi, 201)
y = np.sin(x)
plt.plot(x, y, 'r', linewidth=2);
Mehrere Graphen in einem Bild
z = np.cos(x**2)
plt.plot(x, y)
plt.plot(x, z);
Hier gibt es nicht genug Punkte
x = np.linspace(0, 5*np.pi, 1101)
y = np.sin(x)
z = np.cos(x**2)
plt.plot(x, y)
plt.plot(x, z);
x = np.linspace(-3, 3, 200)
y = 1/(x**3 + 2*x**2 + 4)
plt.plot(x, y);
Der Sprung von $-\infty$ nach $\infty$ muss weg:
b = y>0
c = y<0
plt.plot(x[b], y[b])
plt.plot(x[c], y[c], 'b');
Farben als Kürzel (Alternativen später)
b | g | r | c | m | y | k | w |
---|---|---|---|---|---|---|---|
blue | green | red | cyan | magenta | yellow | black | white |
Oben und unten abschneiden
b = y>0
c = y<0
plt.plot(x[b], y[b], 'b')
plt.plot(x[c], y[c], 'b')
plt.axis(ymin=-1.5, ymax=1.5);
Größe ändern, Überschrift
plt.figure(None, (15, 4))
plt.plot(x, np.sin(3*np.pi*x), label='$\sin(3\pi x)$')
plt.title("Graph der Sinusfunktion")
plt.legend(loc="upper right");
Dasselbe objektorientiert
fig = plt.figure(None, (10,6))
ax1, ax2 = fig.subplots(2,1, sharex=True)
ax1.plot(x, np.sin(3*np.pi*x), 'w')
ax2.plot(x, np.cos(3*np.pi*x), 'w')
ax1.set_title("Sinus")
ax2.set_title("kein Sinus")
for a in [ax1, ax2]:
a.set_facecolor('k')