Issue
I'm having difficulty writing text onto a Basemap
that is inside a PySide QWidget
. From what I've found online, people do the following:
import matplotlib.pyplot as plt
...
plt.text(x, y, 'some text')
But when I try something like that with my code, it pops up another window with the text, not on the map within the widget.
Basemap doesn't have a method like text()
so I've been stumped with what to do. Anybody have ideas on how to draw text to a basemap in this scenario?
Full Code:
'''
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_())
Solution
To get text on a basemap plot you would add that text to the axes. You have stored those in self.axes
.
Here you would create an empty text
# 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")
and later update the text's position and string:
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()
Answered By - ImportanceOfBeingErnest
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.