使用赛璐珞相机 python 动画更新 xaxis

Updating xaxis with celluloid camera python animation

提问人:Liv 提问时间:11/16/2023 最后编辑:digitalextremistLiv 更新时间:11/16/2023 访问量:14

问:

我想制作一个动画,其中 x 轴更新,使用 celluloid.camera.snap() 由于时间序列很长,动画。FuncAnimation 并不理想(现在已经运行了,所以甚至不确定它是否有效)。 我试图添加一些东西。但是 x 轴保持不变,线条在绘图中移动。由于我想绘制几年的数据,我希望它将 x 轴更新为我绘制每帧的 5 天。blit = False


start = datetime(2014,12,1,0,0,0)
ds_start = datetime(2014,12,12,0,0,0)
time = pd.date_range(start = start, freq = 'd', periods = len(RS.time))
myFmt = mdates.DateFormatter('%d')

for t in range(20):
    fig = plt.figure(figsize = (11,8),)
    camera = Camera(fig)
    plt.subplots_adjust(hspace=0.4)
    gs = fig.add_gridspec(5, 13)
    ax = fig.add_subplot(gs[0:3, :9], projection=ccrs.PlateCarree())
    ax1 = fig.add_subplot(gs[3:, :9])
    ax3 = fig.add_subplot(gs[0:3, 10:])
   
    ax.set_extent((162.,169.94, -78.5, -74.25), crs=ccrs.PlateCarree())
       
    gl = ax.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='black', alpha=0.5, linestyle='--', draw_labels=True)
    gl.top_labels = False
    gl.left_labels = True
    gl.right_labels=False
    gl.xlines = True
    gl.xlabel_style = {'color': 'black', 'size': 15}
    gl.ylabel_style = {'color': 'black', 'size': 15}
    # ax.text(163,-74,time[t].strftime("%d-%m-%Y"), fontsize = 15)
    
    
    
    ax2 = ax1.twinx()
    
    
    for t in range(15,25):# short period just to test the function
        ax.set_title(time[t].strftime("%d-%m-%Y"), fontsize = 15)
        im = ax.contourf(RS.Longitude,RS.Latitude,RS.SeaIceConcentration[t], cmap = 'PuBu_r', levels = np.arange(0,101,10))
        ds.SA.sel(depth = 75,time = slice(time[t-2],time[t+3])).plot(ax=ax2, c = 'darkblue', alpha = 0.3)
        ds.CT.sel(depth = 75,time = slice(time[t-2],time[t+3])).plot(ax=ax1, c = 'darkgreen', alpha = 0.3)
            
        ds.T.sel(depth =115,time = slice(time[t-2],time[t+3])).plot(ax=ax1, c = 'olivedrab', alpha = 0.3)
        ds.T.sel(depth =155,time = slice(time[t-2],time[t+3])).plot(ax=ax1, c = 'olivedrab', alpha = 0.4)
        ds.T.sel(depth =195,time = slice(time[t-2],time[t+3])).plot(ax=ax1, c = 'olivedrab', alpha = 0.5)
        ds.T.sel(depth =235,time = slice(time[t-2],time[t+3])).plot(ax=ax1, c = 'olivedrab', alpha = 0.6)
        
        ds.SA.sel(depth =275,time = slice(time[t-2],time[t+3])).plot(ax=ax2, c = 'darkblue', alpha = 0.7)
        ds.CT.sel(depth =275,time = slice(time[t-2],time[t+3])).plot(ax=ax1, c = 'darkgreen', alpha = 0.7)
            
        ds.SA.sel(depth = 668,time = slice(time[t-2],time[t+3])).plot(ax=ax2, c = 'darkblue', alpha = 1)
        ds.CT.sel(depth = 668,time = slice(time[t-2],time[t+3])).plot(ax=ax1, c = 'darkgreen', alpha = 1)
            
        ax1.axvline(time[t], c = 'black',alpha = 0.3)
        ax1.axvline(time[t+1], c = 'black',alpha = 0.3)
        
        
        
        ax1.set_ylabel('Con. Temperature [$\degree$C]', c = 'darkgreen')
        ax1.set_xlabel(' ')
        ax1.tick_params(axis='y', colors='darkgreen')
        ax2.set_ylabel('Abs. Salinity [g/kg]', c = 'darkblue')
        ax2.tick_params(axis='y', colors='darkblue')
        ax2.invert_yaxis()
        ax1.set_title('  ')
        ax2.set_title('  ')
        ax1.set_xlim(time[t-2],time[t+3])      
        
        wind = MW_days.sel(time = slice(time[t-2],time[t+3]))
        ax3.bar(wind.time,wind.windspeed, color = 'tab:blue')
        ax3.set_ylabel('windspeed [m/s]')
        ax3.xaxis.set_major_formatter(myFmt)
        camera.snap()
        ax1.legend(loc=1,bbox_to_anchor=(1.44, 1.05),title = 'C. Temperature')
        ax2.legend(loc=1,bbox_to_anchor=(1.44, .15), title = 'A. Salinity')
            
    
        
plt.colorbar(im, label = 'Sea Ice Concentration')
ax.stock_img()
animation = camera.animate()

animation.save(figure_path+'t.gif', writer = 'pillow')

即使我在forloop中有ax.set_lim(...),它也不会每帧更新它。 我尝试过很多不同的东西,

试:

ani = FuncAnimation(fig,animate, blit = False)这需要很长时间并且没有解决问题,(将 for 循环放在名为 animate 的函数中)

animation = camera.animate(blit = False )这并没有改变任何事情

Python 动画 相机

评论


答: 暂无答案