Einführung
In Matplotlib kann patheffects verwendet werden, um die Art und Weise zu ändern, wie Pfade gezeichnet werden. TickedStroke ist eine Art von patheffect, der Linien im gestrichelten Stil zeichnet. In diesem Tutorial werden Sie durch die Schritte zur Erstellung von TickedStroke in Matplotlib geführt.
VM-Tipps
Nachdem der VM-Start abgeschlossen ist, klicken Sie in der oberen linken Ecke, um zur Registerkarte Notebook zu wechseln und Jupyter Notebook für die Übung zu nutzen.
Manchmal müssen Sie einige Sekunden warten, bis Jupyter Notebook vollständig geladen ist. Die Validierung von Vorgängen kann aufgrund von Einschränkungen in Jupyter Notebook nicht automatisiert werden.
Wenn Sie bei der Lernphase Probleme haben, können Sie Labby gerne fragen. Geben Sie nach der Sitzung Feedback, und wir werden das Problem für Sie prompt beheben.
Anwendung von TickedStroke auf Pfade
In diesem Schritt werden wir TickedStroke auf Pfade anwenden.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path
import matplotlib.patheffects as patheffects
fig, ax = plt.subplots(figsize=(6, 6))
path = path.Path.unit_circle()
patch = patches.PathPatch(path, facecolor='none', lw=2, path_effects=[
patheffects.withTickedStroke(angle=-90, spacing=10, length=1)])
ax.add_patch(patch)
ax.axis('equal')
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
plt.show()
Dieser Code erstellt einen Einheitskreis mit dem TickedStroke-Patheffekt.
Anwendung von TickedStroke auf Linien
In diesem Schritt werden wir TickedStroke auf Linien anwenden.
fig, ax = plt.subplots(figsize=(6, 6))
ax.plot([0, 1], [0, 1], label="Line",
path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)])
nx = 101
x = np.linspace(0.0, 1.0, nx)
y = 0.3*np.sin(x*8) + 0.4
ax.plot(x, y, label="Curve", path_effects=[patheffects.withTickedStroke()])
ax.legend()
plt.show()
Dieser Code erstellt eine Linie und eine Kurve mit dem TickedStroke-Patheffekt.
Anwendung von TickedStroke auf Konturplots
In diesem Schritt werden wir TickedStroke auf Konturplots anwenden.
fig, ax = plt.subplots(figsize=(6, 6))
nx = 101
ny = 105
## Set up survey vectors
xvec = np.linspace(0.001, 4.0, nx)
yvec = np.linspace(0.001, 4.0, ny)
## Set up survey matrices. Design disk loading and gear ratio.
x1, x2 = np.meshgrid(xvec, yvec)
## Evaluate some stuff to plot
obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2
g1 = -(3*x1 + x2 - 5.5)
g2 = -(x1 + 2*x2 - 4.5)
g3 = 0.8 + x1**-3 - x2
cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16],
colors='black')
ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)
cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')
plt.setp(cg1.collections,
path_effects=[patheffects.withTickedStroke(angle=135)])
cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
plt.setp(cg2.collections,
path_effects=[patheffects.withTickedStroke(angle=60, length=2)])
cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
plt.setp(cg3.collections,
path_effects=[patheffects.withTickedStroke(spacing=7)])
ax.set_xlim(0, 4)
ax.set_ylim(0, 4)
plt.show()
Dieser Code erstellt einen Konturplot mit dem TickedStroke-Patheffekt.
Richtung/Seite der Striche
In diesem Schritt werden wir die Seite der Striche ändern.
fig, ax = plt.subplots(figsize=(6, 6))
line_x = line_y = [0, 1]
ax.plot(line_x, line_y, label="Line",
path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)])
ax.plot(line_x, line_y, label="Opposite side",
path_effects=[patheffects.withTickedStroke(spacing=7, angle=-135)])
ax.legend()
plt.show()
Dieser Code erstellt eine Linie mit Strichen auf beiden Seiten.
Zusammenfassung
In diesem Tutorial wurde gezeigt, wie man in Matplotlib TickedStroke erstellt. Indem man diese Schritte befolgt, kann man TickedStroke auf Pfade, Linien und Konturplots anwenden. Man kann auch die Richtung/Seite der Striche anpassen.