Maintaining one colorbar for maptlotlib FuncAnimation

后端 未结 3 1756
醉酒成梦
醉酒成梦 2020-12-18 10:42

I\'ve made a script which uses matplotlib\'s FuncAnimation function to animate a series of contour plots for paraboloid surface functions. I\'d lik

3条回答
  •  北海茫月
    2020-12-18 11:12

    Here is a lazy way to add colorbar. Instead of updating colorbar object, this code delete and create all objects in fig.

    N = 10 # number of color steps
    vmin, vmax = 0, 10 # this should be min and max of z
    V = np.linspace(vmin, vmax, N) 
    
    fig = plt.figure()
    def animate(index):
        fig.clear()
        ax = plt.subplot(1,1,1)
        zi = ml.griddata(x, y, zlist[index], xi, yi, interp='linear')
        contourplot = ax.contourf(xi, yi, zi, V, cmap=plt.cm.hsv,origin='lower')
        cbar = plt.colorbar(contourplot)
        ax.set_title('%03d'%(index))
        return ax
    

提交回复
热议问题