Lektion 8

In [1]:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt

Zeichnen und Interpolation

In [2]:
a = [1, 2, 3, 4]
b = [3, 2, 0, 1]

Polygonzug zeichnen

In [3]:
fig = plt.figure(1)        # erzeugt ein Fenster fuer Graphik
ax = fig.add_subplot(111) # 'ax' ist das Koordinatensystem
ax.plot(a, b,'x');
In [4]:
bb = np.interp(2.5, a, b)
ax.plot(2.5, bb, 'rd');
In [5]:
aas = np.array([2.5, 2.6, 2.7])
bbs = np.interp(aas, a, b)
ax.plot(aas, bbs, 'mp');

Verschönerung

In [6]:
ax.set_xlim([1, 3])
ax.set_xlabel('x Achse')
ax.set_ylabel('meine Funktion')
ax.axis([-1, 4, 0, 5]);

x und y Achse vertauscht

In [7]:
fig = plt.figure(2)
plt.plot(b, a)
Out[7]:
[<matplotlib.lines.Line2D at 0x7fc28f7f7eb8>]

Logarithmisch skalierte Achsen

In [8]:
x = np.arange(1000) + 1;
y = np.log(x);
fig = plt.figure()
plt.plot(x, y);
In [9]:
fig = plt.figure(3)
ax = plt.axes(xlabel="x", ylabel="y", title="Log Funktion")
ax.title.set_fontsize(20);
ax.xaxis.label.set_fontweight("bold");
ax.yaxis.label.set_fontstyle("italic");
ax.semilogx(x, y);
In [10]:
y = x**3.3
plt.figure(13)
plt.axes(xlabel="x", ylabel="y", title="Potenz Funktion")
plt.loglog(x,y)
Out[10]:
[<matplotlib.lines.Line2D at 0x7fc28f764d30>]
In [11]:
# Alternativ
plt.figure(111)
plt.xlabel("x")
plt.ylabel("y") 
plt.title("Potenz Funktion",fontsize=20)
plt.xticks(fontsize=20)
plt.loglog(x,y);

Tortengraphik

In [12]:
tiere    = ["Frösche", "Igel", "Hunde", "Katzen" ];

anzahlen = [15,         30,     45,      10      ];

farben   = ["yellow",  "gold", "blue", "coral"  ];

fig = plt.figure(20)
plt.pie(anzahlen, labels=tiere, colors=farben, shadow=True,
        startangle=90, explode=[ 0, 0, 0, 0.5 ]);
plt.axis("equal");

Flächen füllen

In [13]:
plt.figure(30)
dreieck = [[0,0], [1,0], [0,1], [0,0]]
plt.plot([0,1,0,0], [0,0,1,0])
plt.axis(xmin=-.01,ymin=-.01);
In [14]:
plt.figure(31)
dreieck = [[0,0], [1,0], [0,1], [0,0]]
plt.fill([0,1,0,0], [0,0,1,0],color='red')
plt.axis(xmin=-.01,ymin=-.01);
In [15]:
x = np.linspace(0,1.8*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
In [16]:
plt.figure(32)
plt.fill(x,y1)
plt.plot(x,y1,'xr')
#plt.fill(y1,y2,'r')
#plt.plot(y1,y2,'co')
plt.axis('equal')
Out[16]:
(-0.2827433388230814, 5.937610115284709, -1.0997554324061753, 1.0989334712531)

3D-Grafik

In [17]:
from mpl_toolkits.mplot3d import Axes3D
from itertools import product, combinations
In [18]:
fig = plt.figure(100)
ax = fig.add_subplot(111, projection='3d')

phi = np.linspace(0,2*np.pi,20).reshape(-1,1)
theta = np.linspace(0,np.pi,20).reshape(1,-1)

x = np.cos(phi) * np.sin(theta)
y = np.sin(phi) * np.sin(theta)
z = np.ones_like(phi) * np.cos(theta)

ax.plot3D(x.flatten(),y.flatten(),z.flatten(),'r')
my_col = plt.cm.hsv((x+1)/2)
ax.plot_surface(x,y,z,facecolors=my_col)

#Wuerfel
r = [-1, 1]
for s, e in combinations(np.array(list(product(r,r,r))), 2):
    if np.sum(np.abs(s-e)) == r[1]-r[0]:
        ax.plot3D(*zip(s,e), color="b")
ax.set_aspect('equal')

Was passiert bei der Konstruktion des Würfels?

In [19]:
[xx for xx in product(r,r,r)]
Out[19]:
[(-1, -1, -1),
 (-1, -1, 1),
 (-1, 1, -1),
 (-1, 1, 1),
 (1, -1, -1),
 (1, -1, 1),
 (1, 1, -1),
 (1, 1, 1)]
In [20]:
[xx for xx in combinations( [1,2,3],2)]
Out[20]:
[(1, 2), (1, 3), (2, 3)]
In [21]:
[xx for xx in zip((-1, -1, -1), (1, 2, 3))]
Out[21]:
[(-1, 1), (-1, 2), (-1, 3)]
In [22]:
xx = [*zip((-1, -1, -1), (1, 2, 3))]
xx
Out[22]:
[(-1, 1), (-1, 2), (-1, 3)]