0

I'm trying to create a button which plots a random point on a matplotlib Basemap. Only problem is that I want the previously plotted points to be removed (i.e. there should only be one point on the map at a given time.)

I thought I that after initializing an empty Basemap, I could save it into a variable (newMap = self.map) so when I want to plot a new point I could just plot using newMap (i.e. newMap.plot(x, y, 'o')). From what I can tell, the change persists in the original basemap self.map.

Anybody know how to 'clear' a Basemap so that previously plotted points don't persist?

Full code:

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()
        self.canvas.draw()
        self.show()

    def changeMap(self, lat, lon):
        # I'm saving the supposedly 'clean' Basemap to a variable and plotting on that
        newMap = self.map
        x, y = newMap(lon, lat)
        newMap.plot(x, y, 'o')
        self.canvas.draw()


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

    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_())
Arda Arslan
  • 1,331
  • 13
  • 24

1 Answers1

2

Removing an old point and drawing a new one is the same as just changing the position of one single dot.

So you'd create an empty plot and store it in a class variable, self.point, = self.map.plot([],[], 'o')

class MapWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MapWidget, self).__init__(parent)
        # ....
        self.point, = self.map.plot([],[], 'o')
        self.canvas.draw()
        self.show()

You then use your map (note that newmap = self.map is not a copy, it's a reference to the same map, so no need to create new variables here) and set new data to the plot stored in self.point.

def changeMap(self, lat, lon):
    x, y = self.map(lon, lat)
    self.point.set_data(x, y)
    self.canvas.draw_idle()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712