gpt4 book ai didi

python - 将 mpl_toolkits.basemap Canvas 添加到 Pyside

转载 作者:行者123 更新时间:2023-12-01 05:40:56 25 4
gpt4 key购买 nike

我使用 mpl_toolkits.basemap 生成了一个 map ,它可以工作。

但是,在尝试将其集成到 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()

最佳答案

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

关于python - 将 mpl_toolkits.basemap Canvas 添加到 Pyside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17556374/

25 4 0
文章推荐: java - 接缝FORM问题
文章推荐: jquery - 未捕获的类型错误 : Object # has no method 'validate'