我在嵌入式 pyside 窗口中有一张来自 mpl_toolkits.basemap 的地图。当我调整窗口大小或使用 NavigationToolbar 中的缩放/平移按钮时,背景未正确更新。
这可能是什么原因?
当前代码是:
class geoDialog(QtGui.QDialog, Ui_mplDialog):
def __init__(self, parent=None):
super(geoDialog, self).__init__(parent)
# Basic preparation for an embedded matplotlib
self.setupUi(self)
#self.fig = Figure(figsize=(600,600), dpi=72, facecolor=(1,1,1), edgecolor=(1,1,1))
#self.fig = Figure(figsize=(5,8), dpi=72)
self.fig = Figure()
#self.setFixedSize(600, 600)
#fig.set_picker(picker)
self.canvas = FigureCanvas(self.fig)
#self.verticalLayoutExtra = QtGui.QVBoxLayout()
self.verticalLayout.addWidget(self.canvas)
#self.verticalLayoutExtra.addWidget(self.canvas)
#self.ax = self.fig.add_axes([0.125,0.175,0.75,0.75]) #, axisbg='0.3')
self.ax = self.fig.add_axes([0,0,1,1])
self.ax.set_title('Geographical data')
self.fig.set_frameon(False)
#fig.set_alpha(self, alpha)
#self.canvas.setSizePolicy(QtGui.QSizePolicy.Expanding,
# QtGui.QSizePolicy.Expanding)
#self.canvas.updateGeometry()
#from matplotlib.pyplot import Scatter
self.navigation_toolbar = NavigationToolbar(self.canvas, self)
self.verticalLayout.addWidget(self.navigation_toolbar, 0)
from mpl_toolkits.basemap import Basemap
from geopy import geocoders
#Draw cities
gn = geocoders.Google()
lats = []
lons = []
cities = []
allEntities = session.query(Entity).all()
#cities = ['Goirle, 5051','Neder Horst, 1394' , 'New Castle, NE1']
for e in allEntities:
if e.entityCity is not None:
cities.append(str(e.country + ", " + e.entityCity + ", " + e.entityZipCode))
for city in cities:
print city
place, (lat, lng) = gn.geocode(city)
lats.append(lat)
lons.append(lng)
if len(cities) > 0:
llcrnrlatMin = min(lats) - 5
urcrnrlatMax = max(lats) + 5
llcrnrlonMin = min(lons) - 5
urcrnrlonMax = max(lons) + 5
ratio = (urcrnrlonMax - llcrnrlonMin) / (urcrnrlatMax - llcrnrlatMin)
print ratio, urcrnrlonMax, llcrnrlonMin, urcrnrlatMax, llcrnrlatMin
if ratio > 1:
urcrnrlatMax = urcrnrlatMax * (1 + (ratio - 1)/2)
if urcrnrlatMax > 70: urcrnrlatMax = 70
if urcrnrlatMax < -70: urcrnrlatMax = -70
llcrnrlatMin = llcrnrlatMin * (1 - (ratio - 1)/2)
if llcrnrlatMin > 70: llcrnrlatMin = 70
if llcrnrlatMin < -70: llcrnrlatMin = -70
else:
urcrnrlonMax = urcrnrlonMax * (1 + (ratio - 1)/2)
if urcrnrlonMax > 180: urcrnrlonMax = 179
if urcrnrlonMax < -180: urcrnrlonMax = -179
llcrnrlonMin = llcrnrlonMin * (1 - (ratio - 1)/2)
if llcrnrlonMin > 180: llcrnrlonMin = 179
if llcrnrlonMin < -180: llcrnrlonMin = -179
else:
llcrnrlatMin = 30
urcrnrlatMax = 65
llcrnrlonMin = -20
urcrnrlonMax = 40
print urcrnrlonMax, llcrnrlonMin, urcrnrlatMax, llcrnrlatMin
self.m = Basemap(projection='merc', llcrnrlat=llcrnrlatMin, urcrnrlat=urcrnrlatMax, \
llcrnrlon=llcrnrlonMin, urcrnrlon=urcrnrlonMax, resolution='i', ax=self.ax)
self.m.drawcoastlines()
self.m.drawcountries()
self.m.drawstates()
self.m.fillcontinents(color='coral', lake_color='aqua')
if len(cities) > 0:
x,y = self.m(lons, lats)
print x, y
self.m.plot(x, y, 'bo', picker = True)
# plot the names of those five cities.
for name, xpt, ypt in zip(cities,x,y):
self.ax.text(xpt+5000,ypt+5000,name)
self.m.drawmapboundary(fill_color='aqua')
self.updatesEnabled()
def resizeEvent(self, event):
print "resize"
#self.update()
#self.canvas.update()
frame.environmentGeneral_tab.setBackgroundRole(QPalette.Window)
self.setBackgroundRole(QPalette.Window)
self.update()