Mike C.
Mike C.

Reputation: 1921

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. 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.

Upvotes: 2

Views: 8889

Answers (2)

Buzz
Buzz

Reputation: 1412

I would suggest to use setData to control ScatterPlotItem data instead of creating/adding/removing the object each time the mouse is moving:

# 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.

Upvotes: 1

luddek
luddek

Reputation: 879

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)

Upvotes: 8

Related Questions