matplotlib.animation列表

时间:2018-02-08 12:30:33

标签: python animation matplotlib

我是matplotlib的新手,我正在尝试制作一个grib文件列表的动画。

我写了这段代码:

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()

它可以打开一个我必须关闭的窗口,然后打开一个新窗口。

我想使用matplotlib.animation的漂亮动画:我会改变地图上的凹凸并将其显示为时间序列。

我试试这个:

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()

但我收到了这个错误:

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

如果我需要告诉Python使用函数animate()迭代思考一些变量,我得到了什么。 我不熟悉定义函数,我陷入了代码的这一部分:我无法理解我的代码在哪里错了......

有些帮助吗? 非常感谢!!

1 个答案:

答案 0 :(得分:0)

要摆脱错误,请设置array(2) { [0]=> string(30) "/something/" [1]=> string(71) "/something\?t[p]/" } PHP Notice 'yii\base\ErrorException' with message 'Array to string conversion'

除此之外,动画当前不会为任何动画制作动画,因为对于每个帧,您都会遍历所有文件。相反,您希望每帧显示一个文件。

blit=False