How do I color an Area plot?

I have an area plot where I would like to use colors to make it very clear when

  • values are positive or negative
  • values are large (absolutely) or not.

How can I show positive values with green color and negative values with read color?
How can I use a colormap with the area chart to indicate large (absolute) values?

image

import numpy as np
import holoviews as hv
import panel as pn
hv.extension('bokeh')
xs = np.linspace(0, np.pi*4, 40)
area = hv.Area((xs, np.sin(xs)))
pn.panel(area).servable()

@Marc - have you found an answer to this question? It’s coming up for me now.

My first reaction was to try

xs = np.linspace(0, np.pi*4, 40)
ys = np.sin(xs)
hv.Area((xs, [max(v,0) for v in ys]))*hv.Area((xs, [min(v,0) for v in ys]))

but this does something interesting for y=0…

1 Like

I thought something like:
https://holoviews.org/user_guide/Style_Mapping.html

import numpy as np
import holoviews as hv
import panel as pn
hv.extension('bokeh')
xs = np.linspace(0, np.pi*4, 40)
area = hv.Area((xs, np.sin(xs)))

area.apply.opts(color=hv.dim('y').bin([-2, 0, 2], ["black", "red"]))

But yields:
Mapping a dimension to the “color” style option is not supported by the Area element using the bokeh backend. To map the “y” dimension to the color use a groupby operation to overlay your data along the dimension.

So maybe a groupby

hvplot seems to work:

import numpy as np
import holoviews as hv
import panel as pn
import pandas as pd
hv.extension('bokeh')
xs = np.linspace(0, np.pi*4, 40)
ys = np.sin(xs)
zs = ys > 0
df = pd.DataFrame({"x": xs, "y": ys, "z": zs})

ds = hv.Dataset(df)
ds.to.area("x", "y", "z").overlay()

import hvplot.pandas
df.hvplot.area('x', 'y', by='z')

1 Like