1

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, save it a file, and create a new plot for the next timestep (using a for loop).

For example:


map_init  #[Basemap Instance]
extra_shapes  #[Basemap.readshapefile object]

for i in range(timesteps):
    #plot the weather data for current timestep to current plot
    map_init.imshow(data[i])

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

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

    # add a colorbar
    pyplot.colorbar()

    # Save the figure
    pyplot.savefig(filepath)

    #close figure and loop again (if necessary)
    pyplot.clf()

The problem lies with pyplot.clf()

The code works except for one thing. Only the first plot comes out as expected. Every subsequent plot is missing the extra_shapes (ie no county boundaries). I do not understand the relation between presence of pyplot.clf() and the failure of pyplot.Polygon() ?

If removed, extra_shapes is plotted, but then every plot has multiple colorbars (depending on the value of i). The only reason pyplot.clf() is there is to avoid having 18 colorbars in the final plot. Is there a way to force one and only one colorbar per plot?

1
  • The logic behind the original loop was a rather hasty attempt to make completely separate plots of each timestep. Originally, map_init was assigned inside the loop as well as extra_shapes. extra_shapes is a rather large dataset (as it contains the boundaries for each county in the United States and related territories). Assigning extra_shapes each iteration of the loop became very time costly (the same with map_init). So moving those two assignments outside of the loop, and everything broke. Commented Aug 11, 2010 at 21:07

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much for the help. I couldn't get my original loop to work as expected (even with the suggestion from your first example). However, the second example works like a charm. It actually makes more sense than my original loop. The img data is really the only thing changing between each timestep. ps. see edited question above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.