Search code examples
pythonpandasmatplotlib-animation

How do I update 3D quiver FuncAnimation from a data in csv file?


I need help on how to update 3D quiver head value from csv currently, I have a csv file with 3 columns of x, y, z axis that I want to update into u,v,w variable on my python code.

The problem I am facing is that the result animation does not animate properly. It is a static picture shown below.

https://i.sstatic.net/Os8FX.png

I'm sure that I must have messed up on how to iterate/read the csv data file.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots(subplot_kw=dict(projection="3d"))

# Reading the data from a CSV file using pandas
repo = pd.read_csv('test.csv',sep=',',header=0)
data = np.array((repo['x'].values, repo['y'].values, repo['z'].values))

def get_arrow():
    x = 0
    y = 0
    z = 0
    u = data[0][:]
    v = data[1][:]
    w = data[2][:]
    return x,y,z,u,v,w

quiver = ax.quiver(0,0,0,0,0,0)

ax.set_xlim(-.5, .5)
ax.set_ylim(-.5, .5)
ax.set_zlim(-.5, .5)

def update(theta):
    global quiver
    quiver.remove()
    quiver = ax.quiver(*get_arrow())


ani = FuncAnimation(fig, update, frames=np.linspace(0,2*np.pi,200), interval=50)
plt.show()

Solution

  • I'm not sure how the arrows will behave because the data is unknown, but I tried to create them with sample data. I've also included a library for GIF images and jupytert, so you can modify it to suit your environment.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib.animation import FuncAnimation
    import random
    from IPython.display import HTML
    from matplotlib.animation import PillowWriter
    
    random.seed(20210110)
    
    repo = (pd.DataFrame({'x':np.random.randint(0,5, size=50),
                         'y':np.random.randint(0,5, size=50),
                         'z':np.random.randint(0,5, size=50)}))
    data = np.array((repo['x'].values, repo['y'].values, repo['z'].values))
    
    fig, ax = plt.subplots(subplot_kw=dict(projection="3d"))
    
    quiver = ax.quiver([],[],[],[],[],[])
    
    ax.set_xlim(-5, 5)
    ax.set_ylim(-5, 5)
    ax.set_zlim(-5, 5)
    
    def update(i):
        global quiver
        quiver.remove()
        quiver = ax.quiver(0,0,0,data[0][i],data[1][i],data[2][i])
    
    ani = FuncAnimation(fig, update, frames=50, interval=50)
    # plt.show()
    ani.save('./quiver_test_ani.gif', writer='pillow')
    plt.close()
    # jupyter lab
    # HTML(ani.to_html5_video())
    

    enter image description here