Skryge
Skryge

Reputation: 81

How to get a stackplot with PyQtGraph?

The stackplot function from Matplotlib library can be used as follows :

import matplotlib.pyplot as plt
import numpy as np
import numpy.random as npr

x = np.linspace(0,10,50)
y = [npr.rand(50) for i in range(4)]

plt.stackplot(x,y)
plt.show()

I need to use PyQtGraph library for a project :

    import pyqtgraph as pg
    from pyqtgraph.Qt import QtGui
    import numpy as np
    import numpy.random as npr

    x = np.linspace(0,10,50)
    y = [npr.rand(50) for i in range(4)]

    win = pg.GraphicsWindow()
    graph = win.addPlot()
    #stackplot function
    QtGui.QApplication.instance().exec_()

How can I get a stackplot?

Upvotes: 0

Views: 275

Answers (1)

Luke
Luke

Reputation: 11644

Pyqtgraph does not have a built-in stack plot feature, but you could write this yourself just by summing up the plot data before each line to be stacked, and using PlotCurveItem's fillLevel and fillBrush arguments. Example:

import pyqtgraph as pg
import numpy as np

data = np.random.normal(size=(7,10), scale=0.1, loc=1)
stacked = np.cumsum(data, axis=0)

plt = pg.plot()
for i,row in enumerate(stacked):
    curve = plt.plot(row, fillLevel=0, fillBrush=(i,10))
    curve.setZValue(10-i)

Upvotes: 1

Related Questions