Calculate are - varea_stack


I was having a look at the example below from bokeh. I was wondering if its possible to get the area under the graph value for each stack?
Is it also posible to calculate the area under the graph for each y, starting from y = 0?

import numpy as np
import pandas as pd

from bokeh.palettes import brewer
from bokeh.plotting import figure, output_file, show


N = 4
df = pd.DataFrame(np.random.randint(10, 100, size=(15, N))).add_prefix('y')

p = figure(x_range=(0, len(df)-1), y_range=(0, 400))
p.grid.minor_grid_line_color = '#eeeeee'

names = ["y%d" % i for i in range(N)]
p.varea_stack(stackers=names, x='index', color=brewer['Spectral'][N], legend_label=names, source=df)

# reverse the legend entries to match the stacked order



Numpy implements the trapezoidal rule via the trapz method, which will give you the area under a curve.

The area under each incremental segment can be calculated as follows

np.trapz(df, axis=0)

To get the area of each (cumulatively) stacked region w.r.t. the origin, both of the following will give the same answer

np.trapz(df, axis=0).cumsum()


np.trapz(df.cumsum(axis=1), axis=0)

If you need to do this in JavaScript in a callback, for example, without the benefit of numpy or Python callbacks, you could compute manually easily as the trapezoidal rule is simple for uniformly spaced data.