fill only positive values in PlotCurveItem.py

409 views
Skip to first unread message

Anthony Torlucci

unread,
Aug 25, 2015, 2:35:40 PM8/25/15
to pyqtgraph
Hello all,

I'm not even sure if I should do this, but is there anyway to subclass PlotCurveItem so that it only fills under the curve when amplitudes are positive? I'm plotting seismic data and it is common to display the "wiggles" or oscillations with a "variable area" plot, i.e. fill the area under the curve for only positive values.  A good example can be found at: http://wiki.aapg.org/Seismic_data_display.

Currently, I am creating a new array and clipping all values less than zero and then adding that item to the plot in addition to the original array.  While it works, I wonder if this "extra" plot is using valuable resources and causing the system to slow drastically.  Anyone have an idea on how to do it without adding an extra plot curve item?

Tony T.

Vincent Le Saux

unread,
Aug 26, 2015, 2:03:08 AM8/26/15
to pyqtgraph
Hi Anthony,

You should use PlotDataItem instead of PlotCurveItem. PlotDataItem is a sort of "wrapper" of PlotCurveItem (plot only curves) and ScatterPlotItem (plot only symbols) in order to be able to plot lines and symbols. By default, a PlotDataItem does not show any symbols, so that its behaviour is similar to PlotCurveItem. The thing is PlotDataItem comes with additionnal methods. For you, you should provide a fillLevel and fillBrush argument while creating your item. Something like:
data = pg.PlotDataItem([1,2,3,4,5,6],[1,-1,3,-1,3,6], fillLevel=0., fillBrush=pg.mkBrush((255, 0, 0, 150))
should do exactly what you want (check at the documentation of PlotDataItem for more information in the Line Style keywords aguments section : http://www.pyqtgraph.org/documentation/graphicsItems/plotdataitem.html). I've not checked that my proposal actually works, so come back if it is not the case, I will find something else.

Vincent

Anthony Torlucci

unread,
Oct 22, 2015, 6:26:12 PM10/22/15
to pyqtgraph

Vincent,

I apologize it took so long for me to respond.  I tried your method and it wasn't exactly what I was looking for.  I have included some code and images below to show what I am doing - creating a second numpy array (curveP) that is the same as the first with all negative values clipped.

def showCurve(self):
# Clear plot
self.p1.clear()
t = np.linspace(0.0, 1.0, num=1000, endpoint=True)
curve = np.cos(2*np.pi*self.freq*t+self.phi)
#

# to fill only the positive values, a second function is made and clipped to zero
curveP = np.clip(curve, 0, np.max(curve))
cPositiveFill = pg.PlotCurveItem()
self.p1.addItem(cPositiveFill)
cPositiveFill.setData(t, curveP)
if self.fillCheckBox.isChecked():
cPositiveFill.setBrush(0.5)
cPositiveFill.setFillLevel(0.0)
# add the solid line curve after the fill curve or it doesn't display properly
c = pg.PlotCurveItem(pen='k')
self.p1.addItem(c)
c.setData(t, curve)
# pyqtgraph group - Vincent recommendation
#cG = pg.PlotDataItem(pen='r', fillLevel=0, fillBrush=pg.mkBrush(255,0,0,150))
#self.p1.addItem(cG)
#cG.setData(t, curve)

Un-commenting the last 3 lines produces the second image which I believe is what you were showing in your original response.





Finally, thank you for response.  I learned something new.  Even if it wasn't exactly what I needed, it is always good to learn.  Thanks again.

Tony T.

Anthony Torlucci

unread,
Oct 22, 2015, 6:30:06 PM10/22/15
to pyqtgraph


Poul Riis

unread,
Oct 23, 2015, 2:32:15 PM10/23/15
to pyqtgraph
Would you mind to put the full code of your example here?

Poul Riis


Den fredag den 23. oktober 2015 kl. 00.30.06 UTC+2 skrev Anthony Torlucci:


Reply all
Reply to author
Forward
0 new messages