Add stacked lines to stacked area plot
See original GitHub issueIs your feature request related to a problem? Please describe.
I wonder whether stacked lines could be added to stacked area plots (vstack_area()
), which is the default in Seaborn or Plotly for example.
Describe the solution you’d like
Stacked line (vline_stack()
) and area (vstack_area()
) plots take mostly the same arguments. One solution would be to just call vline_stack
every time vstack_area
is called. The solution I wanted to suggest in a PR for discussion is a bit more flexible, as it allows a line_width
argument to be specified, in which case vline_stack
is called.
bokeh/plotting/figure.py
:
def varea_stack(self, stackers, **kw):
result = []
if 'line_width' in kw:
line_width = kw['line_width']
kw.pop('line_width')
result.append(self.vline_stack(stackers=stackers, line_width=line_width, **kw))
for kw in double_stack(stackers, "y1", "y2", **kw):
result.append(self.varea(**kw))
return result
instead of
def varea_stack(self, stackers, **kw):
result = []
for kw in double_stack(stackers, "y1", "y2", **kw):
result.append(self.varea(**kw))
return result
This relies on vline_stack()
and vstack_area()
not diverging in future versions however. You will also notice that the legend has changed: it can be fixed if people think it’s an issue.
Describe alternatives you’ve considered
One could just call vline_stack()
whenever they make a stacked area plot, but other libraries seem to have chosen a different default.
Additional context Without stacked lines
np.random.seed(seed=10)
df = pd.DataFrame(np.random.randint(10, 100, size=(15, 10))).add_prefix('y')
p = figure(x_range=(0, len(df)-1), y_range=(0, 800))
names = ["y%d" % i for i in range(10)]
p.varea_stack(
stackers=names, x='index',
color=brewer['Spectral'][10],
legend_label=names,
source=df,
line_width=2,
alpha=0.5,
)
p.legend.items.reverse()
show(p)
With stacked lines
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (7 by maintainers)
Top GitHub Comments
@harabat Offhand I think I am -1 on adding line properties to the low level
VArea
itself. I think your initial implementation, that confines changes to the higher level “stacked” glyph helpers, is a perfectly reasonable direction to pursue, it just needs to be more careful and thorough about handling the arguments. I’ll have to think about it some more.Regardless, I am definitely reticent to start defaulting the alpha to anything other than 1. There is no default glyph in Bokeh that this is the case for, and apart from being a breaking change, I think it is good to keep the consistent expectation “If I want alpha, I have to set it” true everywhere. I’d be more inclined to follow the seaborn example where the line and area alpha are both 1 but the line color defaults to white to leave “line gaps” between the areas. We would want to start off with a default
line_color=None
in 2.x to avoid breaking changes, then change toline_color="white"
default in Bokeh 3.0 when a breaking change can be made.Turns out I forgot we had identified several options and didn’t actually decide on either.
Rereading through the discussions, I’m more inclined to go with my original approach and your comments on it: confine changes to
varea_stack
, while making sure alllineprops
arguments are accounted for, and add line renderers withline_color=None
by default (to switch toline_color='white'
in 3.0).My gripes with the option of adding a new vstack helper are that it’d be a confusing stack function, compared with the more clear cut
varea_stack
,vline_stack
, andvbar_stack
. Also, I can’t help but side with Seaborn and Pandas on the idea of having lines by default in stacked area charts.Obviously, the above holds for the horizontal equivalents as well.