【问题标题】:Redrawing plot on Basemap in Python在 Python 中重绘底图上的绘图
【发布时间】:2012-06-24 22:00:47
【问题描述】:

我正在底图上绘制散点图。但是,具有此散点图的数据会根据用户输入而变化。我想清除数据(只有数据——不是整个底图)并重新绘制新的散点。

这个问题类似,但没有回答(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()

然后我重复上面的所有代码。 (我还必须为我的面板重做我的盒子尺寸器——我没有包括这些)。

谢谢!

【问题讨论】:

  • 能否请您发布一些代码?我想看看散点图是如何绘制的。

标签: python matplotlib wxpython scatter-plot matplotlib-basemap


【解决方案1】:

matplotlib.pyplot.plot 文档提到 plot() 命令返回具有 xdata 和 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

【讨论】:

  • 你能解释一下plot_handle, = self.map.plot(x,y,'ro')中等号左边逗号的用途吗?我知道这是必要的,但我不明白为什么。
  • @KShores 它从元组中解包第一个元素。见stackoverflow.com/questions/1708292/…
【解决方案2】:

大多数绘图函数返回 Collections 对象。如果是这样,那么您可以使用remove() 方法。在您的情况下,我会执行以下操作:

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

points.remove()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2017-01-13
    • 1970-01-01
    相关资源
    最近更新 更多