3

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!

1
  • Could you please post some code? I'd like to see how the scatter plot is being plotted. Commented Jun 22, 2012 at 18:07

2 Answers 2

4

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.

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

2 Comments

Can you explain what the purpose of the comma on the left side of the equal sign in plot_handle, = self.map.plot(x,y,'ro') is? I know it's necessary, but I don't understand why.
@KShores It unpacks the first element from the tuple. See stackoverflow.com/questions/1708292/…
0

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()

Comments

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.