[英]matplotlib.animation over a list
I am new with matplotlib and I am trying to make an animation of a list of grib files.我是 matplotlib 的新手,我正在尝试制作 grib 文件列表的动画。
I wrote this code:我写了这段代码:
import pygrib
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
import os
m = Basemap(projection='robin', resolution = 'l', area_thresh = 1000.0,
lat_0=0, lon_0=-130)
m.drawcoastlines()
m.drawcountries()
m.fillcontinents(color = 'gray')
m.drawmapboundary()
m.drawmeridians(np.arange(0, 360, 30))
m.drawparallels(np.arange(-90, 90, 30))
for grib in os.listdir("path"):
grbs=pygrib.open(grib)
for grb in grbs:
print grb
lats, lons = grb.latlons()
data=grb.values
x, y = m(lons,lats)
norm=colors.LogNorm())
m.drawcoastlines()
m.drawcountries()
m.fillcontinents(color = 'gray')
m.drawmapboundary()
m.drawmeridians(np.arange(0, 360, 30))
m.drawparallels(np.arange(-90, 90, 30))
cmap = plt.get_cmap('YlOrBr')
cs = m.pcolormesh(x,y,data,shading='flat',cmap=cmap)
plt.colorbar(cs,orientation='vertical', shrink=0.3)
plt.show()
and it works opening a windows that I have to close and after this opening a new one.它可以打开一个我必须关闭的窗口,然后再打开一个新窗口。
I want to use a nice animation with matplotlib.animation
: i would change the grib over a map and showing it as a time series.我想在
matplotlib.animation
使用一个很好的动画:我会改变地图上的 grib 并将其显示为时间序列。
I try this:我试试这个:
m = Basemap(projection='robin', resolution = 'l', area_thresh = 1000.0,
lat_0=0, lon_0=-130)
m.drawcoastlines()
m.drawcountries()
m.fillcontinents(color = 'gray')
m.drawmapboundary()
m.drawmeridians(np.arange(0, 360, 30))
m.drawparallels(np.arange(-90, 90, 30))
#reading the gribs files
griblist = os.listdir("path")
def animate (i):
global griblist
for grib in griblist:
grbs=pygrib.open(grib)
for grb in grbs:
grb
lats, lons = grb.latlons()
x, y = m(lons, lats)
data = grb.values
cmap = plt.get_cmap('YlOrBr')
cs = m.pcolormesh(x,y,data,shading='flat',cmap=cmap)
plt.colorbar(cs,orientation='vertical', shrink=0.5)
plt.title('UV biological effective dose')
anim = animation.FuncAnimation(plt.gcf(), animate,
frames=len(os.listdir("/home/gloria/UV_CAMS")), interval=500, blit=True)
plt.show()
But I got this error:但我收到了这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
return self.func(*args)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 590, in callit
func(*args)
File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 373, in idle_draw
self.draw()
File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 354, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 474, in draw
self.figure.draw(self.renderer)
File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1165, in draw
self.canvas.draw_event(renderer)
File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 1809, in draw_event
self.callbacks.process(s, event)
File "/usr/lib/python2.7/dist-packages/matplotlib/cbook.py", line 563, in process
proxy(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/cbook.py", line 430, in __call__
return mtd(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/animation.py", line 648, in _start
self._init_draw()
File "/usr/lib/python2.7/dist-packages/matplotlib/animation.py", line 1193, in _init_draw
self._draw_frame(next(self.new_frame_seq()))
File "/usr/lib/python2.7/dist-packages/matplotlib/animation.py", line 1214, in _draw_frame
for a in self._drawn_artists:
TypeError: 'NoneType' object is not iterable
What I got if that I need to tell Python to iterate thought some variable using the function animate()
.如果我需要告诉 Python 使用函数
animate()
迭代一些变量,我会得到什么。 I am not fluent with defining functions and I am stuck in this part of the code: I cannot understand where my code is wrong...我不熟悉定义函数,我被困在代码的这一部分:我无法理解我的代码哪里错了......
Some help about this?一些帮助? Many thanks!!
非常感谢!!
To get rid of the error, set blit=False
.要消除错误,请设置
blit=False
。
Apart the animation would currently not animate anything because for each frame you loop over all files.除了动画目前不会对任何内容进行动画处理,因为对于每一帧,您都会遍历所有文件。 Instead you want to show one file per frame.
相反,您希望每帧显示一个文件。
griblist = os.listdir("path")
def animate (i):
grib = griblist[i]
grbs=pygrib.open(grib)
lats, lons = grb.latlons()
x, y = m(lons, lats)
data = grb.values
cmap = plt.get_cmap('YlOrBr')
cs = m.pcolormesh(x,y,data,shading='flat',cmap=cmap)
plt.colorbar(cs,orientation='vertical', shrink=0.5)
plt.title('UV biological effective dose')
anim = animation.FuncAnimation(plt.gcf(), animate,
frames=len(griblist), interval=500)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.