Skip to content Skip to sidebar Skip to footer

Plotting In A Loop (with Basemap And Pyplot)....problems With Pyplot.clf()

I am plotting some weather data for a research project. The plot consists of 18 timesteps. I decided the best way to accomplish this was to make a new plot for each timestep, sav

Solution 1:

Try making a new figure instead of using clf().

e.g.

for i in range(timesteps):
    fig = pyplot.figure()
    ...
    fig.savefig(filepath)

Alternatively (and faster) you could just update the data in your image object (returned by imshow()).

e.g. something like (completely untested):

map_init  #[Basemap Instance]
extra_shapes  #[Basemap.readshapefile object]#plot the weather data for current timestep to current plot
img = map_init.imshow(data[0])

# extra_shapes are county boundaries.  Plot those as polygons
plygn = pyplot.Polygon(map_init.extra_shapes[0])

# Plot the state boundaries (in basemap)
map_init.drawstates()

# add a colorbar
pyplot.colorbar()

for i in range(timestamps):
    img.set_data(data[i])
    plygn.set_xy(map_init.extra_shapes[i])
    pyplot.draw()
    pyplot.savefig(filepath)

However, there's a chance that that method might not play well with basemap. I may also be misremembering the correct way to redraw the figure, but I'm fairly sure it's just plt.draw()...

Hope that helps a bit anyway

Edit: Just noticed that you're drawing your polygons inside of a loop, as well. Updated the second example to properly reflect that.

Post a Comment for "Plotting In A Loop (with Basemap And Pyplot)....problems With Pyplot.clf()"