Mattplotlib FuncAnimation

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
Zeile42
User
Beiträge: 7
Registriert: Montag 21. März 2022, 10:35

Hallo,

bin neu hier im Forum und auch erst am Anfang mit Python.
Vieles findet man ja so im Internet wenn man sucht.
Leider habe ich dazu keine Lösung gefunden.

Also wenn man mit matplotlib eine Linie zeichnet und diese dann animiert wie im Bsp Code, kann man da weitere Linien hinzufügen?
wenn ja, wäre jemand so nett mir einen Tipp zu geben bzw. falls es nicht zu viel aufwand ist kurz ein Bsp zu zeigen?

Prinzipiell würde ich gerne mehrere Linien mittels Quaternion rotieren lassen gleichzeitig.
Vlt gibt es ja auch eine bessere Lösung dazu?

Code: Alles auswählen

 
import numpy as np

from pyquaternion import Quaternion

# import matplotlib
# matplotlib.use('TKAgg')

from matplotlib import pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D


def generate_quaternion():
    q1 = Quaternion.random()
    q2 = Quaternion.random()
    while True:
        for q in Quaternion.intermediates(q1, q2, 20, include_endpoints=True):
            yield q
        # q1, q2 = q2, q1
        q1 = q2
        q2 = Quaternion.random()


quaternion_generator = generate_quaternion()

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.axis('off')


# set up lines and points
x = np.array([4, 4, 4])
y = np.array([4, 4, 4])
z = np.array([4, 4, 4])
lines = ax.plot(x, y, z)

startpoints = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
endpoints = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])

ax.set_xlim((-8, 8))
ax.set_ylim((-8, 8))
ax.set_zlim((-8, 8))
ax.view_init(30, 0)


# initialization function: plot the background of each frame
def init():
    for line in lines:
        line.set_data([], [])
        line.set_3d_properties([])

    return lines


def animate(i):

    q = next(quaternion_generator)
    # print("q:", q)

    for line, start, end in zip(lines, startpoints, endpoints):
        #end *= 5
        start = q.rotate(start)
        end = q.rotate(end)

        line.set_data([start[0], end[0]], [start[1], end[1]])
        line.set_3d_properties([start[2], end[2]])

    fig.canvas.draw()
    #return lines


anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=500, interval=30, blit=False)


plt.show()
Danke

mfg
Antworten