Pfeile in matplotlib animieren
Ich habe eine Zeile in matploltib animiert, die Ausgabe des Codes sieht folgendermaßen aus:
Ich möchte jedoch, dass anstelle dieser Zeile im Code ein Pfeil (dh eine Pfeilspitze am Ende der Zeile) dargestellt wird. Hier ist der Codeausschnitt:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(12, 8))
ax.set(xlim=(0, 104), ylim=(0, 68))
x_start, y_start = (50, 35)
x_end, y_end = (90, 45)
x = np.linspace(x_start, x_end, 50)
y = np.linspace(y_start, y_end, 50)
line, = ax.plot(x, y)
def animate(i):
line.set_data(x[:i], y[:i])
return line,
ani = animation.FuncAnimation(
fig, animate, interval=20, blit=True, save_count=50)
plt.show()
Was muss ich im Code hinzufügen / ändern, damit ich anstelle der Zeile in der Ausgabe einen Pfeil bekomme?
Antworten
Antworten
Sie können ax.arrowden Pfeil zeichnen.
Beachten Sie, dass Sie das Diagramm mit löschen ax.cla()und die Achsengrenzen ax.set()bei jeder Iteration anpassen sollten .
Code
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(12, 8))
ax.set(xlim=(0, 104), ylim=(0, 68))
x_start, y_start = (50, 35)
x_end, y_end = (90, 45)
N = 50
x = np.linspace(x_start, x_end, N)
y = np.linspace(y_start, y_end, N)
def animate(i):
ax.cla()
ax.arrow(x_start, y_start,
x[i] - x_start, y[i] - y_start,
head_width = 2, head_length = 2, fc = 'black', ec = 'black')
ax.set(xlim = (0, 104), ylim = (0, 68))
ani = animation.FuncAnimation(fig, animate, interval=20, frames=N, blit=False, save_count=50)
plt.show()
Animation
Vielleicht schauen Sie hier
Sie können einfach die Linienfunktion in die Pfeilfunktion ändern. Beachten Sie jedoch, dass Sie zuerst den Endpunkt des Pfeils berechnen müssen, da Sie gemäß den Dokumenten nur die Länge dx, dy angeben können. Durch die Verwendung von Pythagoras wäre der Ausgangspunkt sein x[0], y[0]und dx, dyist der Endpunkt übersetzt.
Ich denke, Sie können das jetzt selbst lösen.