How to animate 3d plot_surface in matplotlib

后端 未结 1 1027
我在风中等你
我在风中等你 2020-12-10 05:31

I have created a 3D plot surface from a file and I\'m trying to animate the plot. I have read the examples in the matplotlib webpage and other examples in SO, and notice tha

相关标签:
1条回答
  • 2020-12-10 05:47

    This is possibly not the optimal way, but I found the documentation/examples not sufficient too.

    What I resorted to is the following solution: use animation.FuncAnimation to call a function. In that function clear and redraw, like so:

    from __future__ import division
    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.axes3d as p3
    import matplotlib.animation as animation
    import numpy as np
    
    plot_args = {'rstride': 1, 'cstride': 1, 'cmap':
                 cm.bwr, 'linewidth': 0.01, 'antialiased': True, 'color': 'w',
                 'shade': True}
    
    soln = np.zeros((size, size))
    midpoint = size // 2
    soln[midpoint, midpoint] = 1
    
    #first frame
    X = range(size)
    Y = range(size)
    X, Y = np.meshgrid(X, Y)
    plot = ax.plot_surface(X, Y, soln, **plot_args)
    pam_ani = animation.FuncAnimation(fig, data_gen, fargs=(soln, plot),
                                  interval=30, blit=False)
    
    def data_gen(framenumber, soln, plot):
        #change soln variable for the next frame
        ...
        ax.clear()
        plot = ax.plot_surface(X, Y, soln, **plot_args)
        return plot,
    
    0 讨论(0)
提交回复
热议问题