Search code examples
pythonplotpyqtgraph

Transparency of a filled plot in pyqtgraph


If I'm making a basic filled plot, like in the example from pyqtgraph. How can I edit the transparency of the filled region?

import pyqtgraph as pg
win = pg.GraphicsWindow(title="Basic plotting examples")
p7 = win.addPlot(title="Filled plot, axis disabled")
y = np.sin(np.linspace(0, 10, 1000)) + np.random.normal(size=1000, scale=0.1)
p7.plot(y, fillLevel=-0.3, brush=(50,50,200,100))

You can make custom brushes with mkBrush:

br = pg.mkBrush(color='r')

But there aren't any transparency options that I can find.


Solution

  • You can adjust the transparency of the plot by changing the brush's (r,g,b,a) tuple. The last parameter (a) determines the alpha setting of the filled plot which ranges from 0-255. Setting this value to 0 gives 100% transparency while setting it to 255 gives a 100% opaque fill.

    For your example, changing this line will affect the filled transparency. Setting it to 50 gives you a semi transparent fill

    p7.plot(y, fillLevel=-0.3, brush=(50,50,200,50))
    

    enter image description here

    Setting the value to 200 gives a more opaque fill

    p7.plot(y, fillLevel=-0.3, brush=(50,50,200,200))
    

    enter image description here

    from pyqtgraph.Qt import QtGui, QtCore
    import numpy as np
    import pyqtgraph as pg
    
    app = QtGui.QApplication([])
    
    win = pg.GraphicsWindow(title="Basic plotting examples")
    win.resize(1000,600)
    win.setWindowTitle('pyqtgraph example: Plotting')
    
    # Enable antialiasing for prettier plots
    pg.setConfigOptions(antialias=True)
    
    p7 = win.addPlot(title="Filled plot, axis disabled")
    y = np.sin(np.linspace(0, 10, 1000)) + np.random.normal(size=1000, scale=0.1)
    p7.plot(y, fillLevel=-0.3, brush=(50,50,200,50)) # <-- Adjust the brush alpha value
    p7.showAxis('bottom', False)
    
    ## Start Qt event loop unless running in interactive mode or using pyside.
    if __name__ == '__main__':
        import sys
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()