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 on 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?
I am fairly new to the matplotlib library and am still learning the in and outs. But this has me thoroughly stumped. Any solutions, ideas, etc?