【发布时间】:2019-09-29 15:59:36
【问题描述】:
这是我的代码:
import sys
import numpy as np
import pyqtgraph as pg
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class MyGraph(QWidget):
def __init__(self):
super(MyGraph, self).__init__()
self.resize(600, 600)
pg.setConfigOption('background', 'w')
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
self.pw = pg.PlotWidget(self)
self.pw.plot(x, y, pen=None, symbol='o', symbolBrush='r')
self.plot_btn = QPushButton('Replot', self)
self.plot_btn.clicked.connect(self.plot_slot)
self.v_layout = QVBoxLayout()
self.v_layout.addWidget(self.pw)
self.v_layout.addWidget(self.plot_btn)
self.setLayout(self.v_layout)
def plot_slot(self):
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
# The new data is added to the existed one
self.pw.plot(x, y, pen=None, symbol='o', symbolBrush='r')
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = MyGraph()
demo.show()
sys.exit(app.exec_())
每次点击按钮,我都想清理现有数据并重新绘制新数据,但 PlotWidget 似乎没有相关功能让我这样做。
有什么方法可以清理数据吗?
谢谢!
【问题讨论】: