1

我使用 mpl_toolkits.basemap 生成了一张地图,它可以工作。

但是,在尝试将其集成到 Pyside 之后,我无法将其显示为 QWidget。我没有收到任何错误,程序只是在我等待它启动时挂起。我在网上查过,关于这个问题的文档不多

from PySide.QtGui import (QWidget, QVBoxLayout, QFormLayout, QLineEdit, 
                        QPushButton, QFileDialog, QGroupBox, QApplication)

import sys
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np


class Map(QWidget):

def __init__(self, parent=None):
    super(Map, self).__init__(parent)
    self.setupUI()

def setupUI(self):  
    self.fig = Figure()
    self.canvas = FigureCanvas(self.fig)

    self.layout = QVBoxLayout(self)
    self.mpl_toolbar = NavigationToolbar(self.canvas, self, coordinates = False)
    self.layout.addWidget(self.canvas)
    self.layout.addWidget(self.mpl_toolbar)
    self.axes = self.fig.add_subplot(111)
    self.setLayout(self.layout)

    # make sure the value of resolution is a lowercase L,
    #  for 'low', not a numeral 1
    map = Basemap(projection='robin', lat_0=0, lon_0=-100,
                  resolution='l', area_thresh=1000.0, ax=self.axes)

    map.drawcoastlines()
    map.drawcountries()
    map.fillcontinents(color='green')
    map.drawmapboundary()

    # lat/lon coordinates of five cities.
    lats = [40.02, 32.73, 38.55, 48.25, 17.29]
    lons = [-105.16, -117.16, -77.00, -114.21, -88.10]

    cities=['Boulder, CO','San Diego, CA',
            'Washington, DC','Whitefish, MT','Belize City, Belize']

    # compute the native map projection coordinates for cities.
    x,y = map(lons,lats)
    # plot filled circles at the locations of the cities.
    map.plot(x,y,'bo')

    # plot the names of those five cities.
    for name,xpt,ypt in zip(cities,x,y):
        plt.text(xpt+50000,ypt+50000,name)


    self.canvas.draw()


def main():
    app = QApplication(sys.argv)
    map = Map()

    app.exec_() 

main()  
4

1 回答 1

2

你忘了显示你的小部件。添加self.show()到末尾setupUI

于 2013-07-09T19:37:17.850 回答