简体   繁体   English

如何清除 PYQTGRAPH 中的 ScatterPlotItem

[英]How do I clear a ScatterPlotItem in PYQTGRAPH

I am attempting to move a "cursor" around my graph using ScatterPlotItem and a '+' symbol as the cursor.我正在尝试使用 ScatterPlotItem 和“+”符号作为 cursor 在我的图形周围移动“光标”。 The cursor updates its position perfectly but I cannot figure out how to clear the last instance. cursor 完美地更新了它的 position 但我不知道如何清除最后一个实例。 Here is the line that I use to plot the 'cursor'.这是我用于 plot '光标'的行。

self.cursor2 = self.p2_3.addItem(pg.ScatterPlotItem([self.xx], [self.yy], pen=None, symbol='+', color = 'b'))

I tried self.cursor2.clear() but that didn't work.我试过 self.cursor2.clear() 但这没有用。 Any help is appreciated.任何帮助表示赞赏。

When you call addItem you add the plotdataitem, in this case a scatterplotitem to your plotitem. 当您调用addItem时,您将添加plotdataitem,在这种情况下,将散点图添加到您的plotitem。 To remove it you call removeItem in the same way. 要删除它,您可以用相同的方式调用removeItem。 But you need to keep a reference to the scatterplotitem to do that. 但是您需要保留对散布图的引用才能做到这一点。

Note that addItem doesn't return anything ie your self.cursor2 is None. 请注意,addItem不返回任何内容,即您的self.cursor2为None。

If you want to remove everything from your plot you could call 如果您想删除情节中的所有内容,可以致电

self.p2_3.clear()

otherwise to just remove the scatterplotitem you can do like this 否则,只需删除散点图即可

import pyqtgraph as pg

win = pg.GraphicsWindow()
plotitem = win.addPlot()
scatterplot = pg.ScatterPlotItem([2], [3], symbol='+', color = 'b')

plotitem.addItem(scatterplot)
plotitem.removeItem(scatterplot)

I would suggest to use setData to control ScatterPlotItem data instead of creating/adding/removing the object each time the mouse is moving:我建议使用setData来控制ScatterPlotItem数据,而不是在每次鼠标移动时创建/添加/删除 object:

# This just once:
self.scatterplot = pg.ScatterPlotItem(x=[], y=[], symbol='+', color = 'b')
plotitem.addItem(self.scatterplot)


# On mouse hovering:
self.scatterplot.setData(x=[self.xx], y=[self.yy])

It's way more efficient.这样更有效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM