0

我正在尝试创建一个在 matplotlib 底图上绘制随机点的按钮。唯一的问题是我希望删除以前绘制的点(即在给定时间地图上应该只有一个点。)

我想我在初始化一个空的底图之后,我可以将它保存到一个变量(newMap = self.map)中,所以当我想绘制一个新点时,我可以使用newMap(ie newMap.plot(x, y, 'o'))来绘制。据我所知,更改仍然存在于原始 basemapself.map中。

有人知道如何“清除”底图,以便以前绘制的点不会持续存在吗?

完整代码:

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_())
4

1 回答 1

2

删除一个旧点并绘制一个新点与仅更改一个点的位置相同。

因此,您将创建一个空图并将其存储在类变量中,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()

然后使用您的地图(注意 newmap = self.map 不是副本,它是对同一地图的引用,因此无需在此处创建新变量)并将新数据设置为存储在self.point.

def changeMap(self, lat, lon):
    x, y = self.map(lon, lat)
    self.point.set_data(x, y)
    self.canvas.draw_idle()
于 2017-06-30T15:33:18.510 回答