繁体   English   中英

Matplotlib animation 与 Xarray 数据

[英]Matplotlib animation with Xarray data

我已经坚持了一段时间了。 我尝试 plot 我的数据并为 12 帧设置动画。 这就是我的代码现在的样子。

import xarray as xr
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.animation as animation
from IPython.display import HTML


url = 'https://username:password@nrt.cmems-du.eu/thredds/dodsC/global-analysis-forecast-phy-001-024-monthly?latitude[0:1:2040],longitude[0:1:4319],depth[0:1:49],time[0:1:34],mlotst[0:1:34][0:1:2040][0:1:4319]'
ds = xr.open_dataset(url)
ds.to_netcdf('mlotst.nc') #Make a netcdf-file

cond = (ds.latitude>22) & (ds.latitude<30.5) & (ds.longitude>47) & (ds.longitude<63)
ds = ds.where(cond,drop=True) #Narrowing it down to the coordinates I want

fig = plt.figure(figsize=(10,6))
#ax = plt.axes(xlim=(47, 63), ylim=(22, 31))
levels = range(90)


def animate(time):
    plt.clf()
    fig = ds.isel(time=time).mlotst.plot(levels=levels, figsize=(10,6))

ani = animation.FuncAnimation(fig, animate, range(13), interval=200, blit=False)
plt.show()


FFwriter = animation.FFMpegWriter()
ani.save('anim.mp4', writer = FFwriter)

它只是变成白色的。 请帮忙。

不久前我写了这个问题,但我刚刚看到一条评论,想知道解决方案。 基本上定义 ax =axes 是“只是”,然后我工作了

工作代码:

levels = range(90)
fig, axes = plt.subplots(figsize=[11,8])#ncols=1) #Creating the basis for the plot

def animate(time):
    ds.isel(time=time).mlotst.plot.contourf(levels=levels, ax=axes, add_colorbar= False)

ani = animation.FuncAnimation(fig, animate, 24, interval=400, blit=False)


mld= ds.isel(time=0).mlotst.plot.contourf(levels=levels, ax=axes, add_colorbar= False)
cbar= fig.colorbar(mld)
cbar.set_label('Mixed layer thickness [m]')
fig.suptitle("Mixed layer thickness transect. Persian Gulf and Gulf of Oman Jan -19 to Dec -20", fontsize= 18)

#ani.save('animation.gif', writer='imagemagick', fps = 2) #Save animation as gif-file

HTML(ani.to_jshtml()) #Show the animation in the kernel

希望这对将来的人有所帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM