底图/ PySide QWidget:在地图上绘制文本

时间:2017-07-03 19:03:37

标签: python matplotlib pyside matplotlib-basemap matplotlib-widget

我在将文本写入PySide Basemap内的QWidget时遇到了困难。根据我在网上找到的内容,人们会做以下事情:

import matplotlib.pyplot as plt
...
plt.text(x, y, 'some text')

但是当我用我的代码尝试类似的东西时,它会弹出另一个带有文本的窗口,而不是小部件中的地图。

底图没有类似text()的方法,所以我一直难以理解该怎么做。在这种情况下,是否有人有关于如何在底图中绘制文本的想法?

完整代码:

'''
This program should draw a dot on a random point on Earth and label it 'Point'
You'll probably attempt to do this on line 43 in changeMap()
'''

from PySide import QtCore, QtGui

import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from mpl_toolkits.basemap import Basemap

from random import randint

class MapWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        self.layout = QtGui.QVBoxLayout(self)
        self.mplToolbar = NavigationToolbar(self.canvas, self, coordinates=False)
        self.layout.addWidget(self.canvas)
        self.layout.addWidget(self.mplToolbar)
        self.axes = self.figure.add_subplot(111)
        self.setLayout(self.layout)
        # Create our Basemap
        self.map = Basemap(projection='robin', lon_0=0, ax=self.axes, resolution='c')
        self.map.drawcoastlines()
        self.map.drawcountries()
        # Create empty plot to store random coordinates in for later
        self.points, = self.map.plot([], [], 'o')
        # Draw to screen
        self.canvas.draw()
        self.show()

    def changeMap(self, lat, lon, text):
        x, y = self.map(lon, lat)
        self.points.set_data(x, y)
        # Draw text here

        self.canvas.draw()


class Ui_MainWindow(object):
    def plot(self):
        # Plot on some random coordinate and label it 'Point'
        self.Map.changeMap(randint(-90, 90), randint(-180, 180), 'Point')

    def myChanges(self):
        # Initialize our Basemap
        self.Map = MapWidget()
        self.layoutMap = QtGui.QVBoxLayout(self.widget)
        self.layoutMap.addWidget(self.Map)
        # Plot a new point when the button is clicked
        self.pushButton.clicked.connect(self.plot)

    # The following is all PySide code, not very relevant to the question
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout_2 = QtGui.QHBoxLayout(self.centralwidget)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setMaximumSize(QtCore.QSize(100, 16777215))
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.widget = QtGui.QWidget(self.centralwidget)
        self.widget.setObjectName("widget")
        self.horizontalLayout.addWidget(self.widget)
        self.horizontalLayout_2.addLayout(self.horizontalLayout)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.myChanges()

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Plot", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:2)

要在底图图上获取文本,您可以将该文本添加到轴。您已将这些内容存储在self.axes中。 在这里,您将创建一个空文本

# Create empty plot to store random coordinates in for later
self.points, = self.map.plot([], [], 'o')
self.pointsannotate = self.axes.text(0,0,"", color="crimson")

以后更新文本的位置和字符串:

def changeMap(self, lat, lon, text):
    x, y = self.map(lon, lat)
    self.points.set_data(x, y)
    # Draw text here
    self.pointsannotate.set_position([x,y])
    self.pointsannotate.set_text("{},{}".format(lon, lat))

    self.canvas.draw()