Glori P.
Glori P.

Reputation: 486

matplotlib.animation over a list

I am new with matplotlib and I am trying to make an animation of a list of grib files.

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.

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

Upvotes: 0

Views: 1555

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339280

To get rid of the error, set 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()

Upvotes: 1

Related Questions