将mpl_toolkits.basemap画布添加到Pyside

时间:2013-07-09 19:16:14

标签: python qt qt4 pyside matplotlib-basemap

我使用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()  

1 个答案:

答案 0 :(得分:2)

您忘记展示自己的小部件了。将self.show()添加到setupUI的末尾。