How do I clear a ScatterPlotItem in PYQTGRAPH

柔情痞子 提交于 2020-01-07 02:52:04

问题


I am attempting to move a "cursor" around my graph using ScatterPlotItem and a '+' symbol as the cursor. The cursor updates its position perfectly but I cannot figure out how to clear the last instance. Here is the line that I use to plot the 'cursor'.

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. Any help is appreciated.


回答1:


When you call addItem you add the plotdataitem, in this case a scatterplotitem to your plotitem. To remove it you call removeItem in the same way. But you need to keep a reference to the scatterplotitem to do that.

Note that addItem doesn't return anything i.e. your self.cursor2 is 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)


来源:https://stackoverflow.com/questions/39884931/how-do-i-clear-a-scatterplotitem-in-pyqtgraph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!