0

I am trying to create several images in a series using matplotlib and basemap. Essentially, I am trying to take meteorological model output data and make a plot of it at every timestep. However, I'm trying to do so efficiently and without clearing the figure and redrawing everything (especially the basemap things) at every timestep. Below is the relevant code.

basem = Basemap(llcrnrlon=setlats[1], llcrnrlat=setlats[0],
                       urcrnrlon=setlats[3], urcrnrlat=setlats[2], 
                       projection='mill', resolution='l')
    ...
x,y = basem(lons, lats)
basem.drawcoastlines()
parallels = np.arange(0.,92,10.)
basem.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)
# draw meridians
meridians = np.arange(180.,360.,10.)
basem.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)
basem.drawstates()
basem.drawcountries()
cs = basem.contourf(x,y, plotted_var, levels=clevs, 
    cmap = plt.get_cmap(colorbar), extend="max")
cl = basem.contour(x,y, contoured_var, levels=clevsl, colors='k')
cb = basem.barbs(x[points],y[points], 
        barbs_var_u[points], barbs_var_v[points], 
        barbcolor='k',flagcolor='k', pivot='middle',
        length=4)

...
for time in range(time+1,maxt+1):
    timef = time
    plotted_var = multf(np.array(data2[var])[timef,\
            bounds[0]:bounds[1],bounds[2]:bounds[3]])
    if barbs >=0 or barbs ==-2:
        cb[0].set_UVC(barbs_var_u[points], barbs_var_v[points])
    cs.set_array(plotted_var)
    cl.set_array(contoured_var)
    ...

However, while this changes the barbs, it does not change the contours (either filled or not filled). I'm fairly certain that set_array is not the command I want to use, but I have looked in depth and can't find the set_uvc equivalent. In addition, I have also tried to ravel plotted_var and contoured_var and that also doesn't change. Note: I am only trying to update the z component (colors) of the contoured plot, the x and y need to stay the same.

Laxsnor
  • 857
  • 1
  • 12
  • 21
  • 2
    You will have to remove and re-create the contour artists. There is significant computation done as part of generating the contours so updating in place is not practical. Adding the functionality to update the contours and re-compute in place would be a nice addition to mpl though. – tacaswell Mar 13 '16 at 16:42
  • That's unfortunate. What on earth does set_array do then? I assume looping through the contourf collections and removing then repotting just the contours is the way to go, yes? – Laxsnor Mar 13 '16 at 16:50
  • 2
    I agree (PRs are always welcome!). Yes, looping over and removing contents `ContourSet.collections` is the right thing to do. `set_array` is is inherited from `ScalarMappable` which manages the number -> color mapping. If you set an array that was the same length as the number of contours it _should_ change the contour colors (but I would have to read the code to make sure it actually did that). – tacaswell Mar 13 '16 at 17:02
  • *Blatant self-promotion*: I recently played around with [deleting certain contour lines](http://stackoverflow.com/a/35664089/5067311). Deleting them all should be even easier. Though it might be that my approach is bad from a referencing perspective, or just too complicated. – Andras Deak -- Слава Україні Mar 14 '16 at 00:27

0 Answers0