You forgot to show your widget. Add self.show()
to the end of setupUI
.
Adding mpl_toolkits.basemap canvas to Pyside
-
02-06-2022 - |
Frage
I've generated a map using mpl_toolkits.basemap and it works.
However, after trying to integrate it into Pyside, I'm having trouble displaying it as a QWidget. I'm not getting any errors, the program just hangs while I wait for it to launch. I've looked online, and there isn't much documentation on this subject
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()
Lösung
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow