Question

I am plotting a scatter plot on a Basemap. However, the data with this scatter plot changes based on user input. I would like to clear the data (only the data -- not the entire basemap figure) and re-plot new scatter points.

This question is similar but was not answered (http://stackoverflow.com/questions/8429693/python-copy-basemap-or-remove-data-from-figure)

Currently I am closing the figure with clf(); however, this requires me to re-draw the entire basemap and scatter plot together. On top of this, I am doing all of the redrawing inside of a wx panel. The basemap redraw takes too long and am hoping that there is an easy way to simply re-plot scatter points only.

#Setting up Map Figure 
self.figure = Figure(None,dpi=75) 
self.canvas = FigureCanvas(self.PlotPanel, -1, self.figure) 
self.axes = self.figure.add_axes([0,0,1,1],frameon=False) 
self.SetColor( (255,255,255) ) 

#Basemap Setup 
self.map = Basemap(llcrnrlon=-119, llcrnrlat=22, urcrnrlon=-64, 
                urcrnrlat=49, projection='lcc', lat_1=33, lat_2=45, 
                lon_0=-95, resolution='h', area_thresh=10000,ax=self.axes) 
self.map.drawcoastlines() 
self.map.drawcountries() 
self.map.drawstates() 
self.figure.canvas.draw() 

#Set up Scatter Plot 
m = Basemap(llcrnrlon=-119, llcrnrlat=22, urcrnrlon=-64, 
        urcrnrlat=49, projection='lcc', lat_1=33, lat_2=45, 
        lon_0=-95, resolution='h', area_thresh=10000,ax=self.axes) 

x,y=m(Long,Lat) 

#Scatter Plot (they plot the same thing) 
self.map.plot(x,y,'ro') 
self.map.scatter(x,y,90) 

self.figure.canvas.draw() 

Then I do an some type of update on my (x,y)...

#Clear the Basemap and scatter plot figures
self.figure.clf()

Then I repeat all of the above code. (I also have to redo my box sizers for my panel -- I did not include these).

Thanks!

Was it helpful?

Solution

The matplotlib.pyplot.plot documentation mentions that the plot() command returns a Line2D artist which has xdata and ydata properties, so you might be able to do the following:

# When plotting initially, save the handle
plot_handle, = self.map.plot(x,y,'ro') 
...

# When changing the data, change the xdata and ydata and redraw
plot_handle.set_ydata(new_y)
plot_handle.set_xdata(new_x)
self.figure.canvas.draw()

I haven't managed to get the above to work for collections, or 3d projections, unfortunately.

OTHER TIPS

Most plotting functions return Collections object. If so then you can use remove() method. In your case, I would do the following:

# Use the Basemap method for plotting
points = m.scatter(x,y,marker='o')
some_function_before_remove()

points.remove()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top