我使用互联网上的示例在 wxpython 上为 Matplotlib-Basemap 编写了一个测试 python 程序。启动 GUI 时使用墨卡托投影绘制的地图。在选择各种单选按钮时,用户可以切换到所需的投影。我的问题是,连接到 matplotlib 的导航工具栏似乎仍然包含第一张图像的数据。再多的刷新、更新或实现都无法纠正这一点。当我按下缩放按钮时,第一张图像被缩放。当前图像未更新。欢迎任何帮助或建议。这是我的代码:
from numpy import arange, sin, pi
import matplotlib
from mpl_toolkits.basemap import Basemap
from wx._windows import StatusBar
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import NavigationToolbar2Wx as Toolbar
import wx
class BasemapPanel(wx.Panel):
def __init__(self, parent,projectionString):
wx.Panel.__init__(self, parent)
self.reDraw('merc',72.5,18.4)
def draw(self):
t = arange(0.0, 3.0, 0.01)
s = sin(2 * pi * t)
self.axes.plot(t, s)
def reDraw(self,projectionString,testLon,testLat):
self.figure = Figure(None,dpi=75)
self.canvas = FigureCanvas(self, -1, self.figure)
self.axes = self.figure.add_axes([0,0,1,1],frameon=False)
self.toolbar = Toolbar(self.canvas)
self.toolbar.Realize()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.EXPAND|wx.ALL)
sizer.Add(self.toolbar, 0 , flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
self.SetSizer(sizer)
self.canvas.Refresh(True)
self.Layout()
self.map = Basemap(llcrnrlon=-179, llcrnrlat=-80, urcrnrlon=179,
urcrnrlat=80, projection=projectionString, lat_1=18.2, lat_2=45,
lon_0=-95, resolution='c', area_thresh=10000,ax=self.axes)
self.map.drawcoastlines()
self.map.fillcontinents(color='coral',lake_color='aqua')
self.map.drawmapboundary(fill_color='aqua')
self.map.drawcountries()
self.map.drawstates()
lon, lat = testLon, testLat # Location
xpt,ypt = self.map(lon,lat)
lonpt, latpt = self.map(xpt,ypt,inverse=True)
self.map.plot(xpt,ypt,'bo') # plot a blue dot there
self.axes.text(xpt+100000,ypt+100000,'Location (%5.1f,%3.1f)' % (lonpt,latpt))
self.toolbar.update()
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
self.vbox = wx.BoxSizer(wx.VERTICAL)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
# Radio Boxes
radioListOfProjections = ["merc" , "kav7" , "mbtfpq",
"mill" , "robin",
"lcc" , "moll" , "eqdc"]
self.rb = wx.RadioBox(self, label="Projection ", pos=(20, 210), choices=radioListOfProjections, majorDimension=3,
style=wx.RA_SPECIFY_ROWS)
self.Bind(wx.EVT_RADIOBOX, self.OnSelectingProjection, self.rb)
hbox2.Add(self.rb)
self.vbox.Add(hbox2, proportion=1, flag=wx.LEFT|wx.RIGHT|wx.EXPAND,
border=10)
self.vbox.Add((-1, 10))
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
self.bmPanel = BasemapPanel(self,'merc')
self.bmPanel.draw()
self.bmPanel.SetSize((300,300))
hbox3.Add(self.bmPanel, proportion=1, flag=wx.EXPAND)
self.vbox.Add(hbox3, proportion=0, flag=wx.EXPAND|wx.ALL,
border=10)
self.vbox.Add((-1, 35))
self.SetSizer(self.vbox)
def OnSelectingProjection(self, e):
projectionString = self.rb.GetStringSelection()
self.bmPanel.reDraw(projectionString,45.5,66.6)
self.bmPanel.draw()
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
hbox3.Add(self.bmPanel, proportion=1, flag=wx.EXPAND)
self.vbox.Add(hbox3, proportion=0, flag=wx.EXPAND|wx.ALL,
border=10)
self.vbox.Add((-1, 35))
self.SetSizer(self.vbox)
app = wx.App(False)
frame = wx.Frame(None)
panel = MainPanel(frame)
frame.Size = (650, 700)
frame.Move((400, 250))
frame.Center()
frame.Show()
app.MainLoop()