首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用PlotWidget.plot()后如何清理数据?

使用PlotWidget.plot()后如何清理数据?
EN

Stack Overflow用户
提问于 2019-05-12 13:23:41
回答 1查看 2.1K关注 0票数 2

下面是我的代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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似乎没有相应的功能让我这样做。

有什么方法可以清理数据吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-12 19:46:21

PlotWidget的plot方法生成一个负责绘制的项目,在您的示例中,每次按下按钮时都会创建另一个plot,以便您观察该行为。解决方案是重用

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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.plot = self.pw.plot(x, y, pen=None, symbol="o", symbolBrush="r")

        self.plot_btn = QPushButton("Replot", self, clicked=self.plot_slot)

        v_layout = QVBoxLayout(self)
        v_layout.addWidget(self.pw)
        v_layout.addWidget(self.plot_btn)

    def plot_slot(self):
        x = np.random.normal(size=1000)
        y = np.random.normal(size=1000)
        self.plot.setData(x, y)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56099680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文