2012-06-22 52 views
3

我繪製的底圖的散點圖。但是,散點圖中的數據會根據用戶輸入而改變。我想清除數據(僅數據 - 不是整個底圖數據)並重新繪製新的分散點。重繪上底圖情節在Python

這個問題是類似的,但沒有得到回答(http://stackoverflow.com/questions/8429693/python-copy-basemap-or-remove-data-from-figure)

目前我關閉圖與clf();但是,這需要我重新繪製整個底圖和散點圖。最重要的是,我正在做一個wx面板的重繪。底圖重繪時間太長,希望有一種簡單的方法可以簡單地重新繪製散點。

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

然後,我做我的(X,Y)的某些類型的更新...

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

然後我重複所有上面的代碼。 (我也必須重新爲我的面板重新裝箱我的箱子 - 我沒有包括這些)。

謝謝!

+0

請問您可以發佈一些代碼?我想看看散點圖是如何繪製的。 – stanri

回答

4

matplotlib.pyplot.plot文檔中提到的情節()命令返回具有擴展數據和YDATA性能的Line2D藝術家,所以你也許可以做到以下幾點:

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

我沒有設法得到上面的工作收集,或3d projections,不幸的是。

+0

這工作完美!謝謝! – mcfly

+0

你能解釋一下什麼是逗號等號的'plot_handle,= self.map.plot(X,Y,「RO」)左側的目的是'?我知道這是必要的,但我不明白爲什麼。 –

+1

@KShores它從元組中解壓縮第一個元素。見http://stackoverflow.com/questions/1708292/meaning-of-using-commas-and-underscores-with-python-assignment-operator – stanri

0

大部分繪圖函數返回Collections對象。如果是這樣,那麼你可以使用remove()方法。在你的情況下,我會做以下事情:

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

points.remove()