Matplotlib 3D Scatter Animate Sequential Data

后端 未结 1 1903
灰色年华
灰色年华 2021-01-17 02:32

I\'ve produced the following 3D scatter plot of some accelerometer data:

It\'s pretty basic, but I\'m pleased with the way it looks considering this is my f

1条回答
  •  迷失自我
    2021-01-17 02:49

    There is an example for 3D scatter plots in this question: Matplotlib 3D scatter animations

    In order to let the points appear one by one you would plot the data from the dataframe starting at index 0 up to the current animation index i.

    (df.x.values[:i], df.y.values[:i], df.z.values[:i])
    

    A full example:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.animation
    
    x = np.random.normal(size=(80,3))
    df = pd.DataFrame(x, columns=["x","y","z"])
    
    
    fig = plt.figure()
    ax = fig.add_subplot(111,projection='3d')
    sc = ax.scatter([],[],[], c='darkblue', alpha=0.5)
    
    def update(i):
        sc._offsets3d = (df.x.values[:i], df.y.values[:i], df.z.values[:i])
    
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    ax.set_xlim(-3,3)
    ax.set_ylim(-3,3)
    ax.set_zlim(-3,3)
    
    ani = matplotlib.animation.FuncAnimation(fig, update, frames=len(df), interval=70)
    
    plt.tight_layout()
    plt.show()
    

    0 讨论(0)
提交回复
热议问题