0

I want to plot a graph with multiple curves(although in this particular example all plots are linear), and then plot the sum of the graphs as an additional plot.
I was wondering whether there was a built in way to do it, rather than calculating for each new (x,y) point the coordinates of all other curves at that point and summing them up.
Here's a quick example(it runs as a standalone script):

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

app = QtGui.QApplication([])
win = pg.GraphicsWindow(title="For Test")
plot = win.addPlot(title='Test')

#First plot:
x = [1,3,5,7]
y = [1,2,1,2]
curve1 = plot.plot(x=x, y=y, pen='r')
#Second plot
x = [2,4,6,8]
y = [0.5,4,2,2]
curve2 = plot.plot(x=x, y=y, pen='g')

if __name__ == '__main__':
    app.exec_()

Expected result is something like:
x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [1, ~2, ~4, ~5.5, ~8, ~3.5, 4, 2]
This problem gets a bit harder if there are actual curves. So is there a built in way to do it?

Arthur.V
  • 676
  • 1
  • 8
  • 22

1 Answers1

0

I doubt there is a built-in for this. You have differently spaced points in each curve. What you need to do:

1 - Construct an x vector common to both curves. I suggest

np.linspace(min(x1.min(), x2.min(), max(x1.max(), x2.max(), n_points)

2 - Interpolate the y values of each curve to find values for common x vector

3 - Add the interpolated curves and plot it

See this answer for how to interpolate a curve from known x and y values

Community
  • 1
  • 1
agomcas
  • 695
  • 5
  • 12