Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Legend order backwards for stacked bar plots #551

Open
mullimanko opened this issue Jan 24, 2021 · 11 comments
Open

[Bug] Legend order backwards for stacked bar plots #551

mullimanko opened this issue Jan 24, 2021 · 11 comments
Labels
good first issue Good for newcomers type: bug Something isn't working
Milestone

Comments

@mullimanko
Copy link

This example here shows it well:
https://hvplot.holoviz.org/reference/pandas/bar.html

Enabling the legend in a stacked bar plot displays the legend items in the opposite order of how they are stacked. One would expect them to be in the same order.

Unbenannt

This might be somehow related:
bokeh/bokeh#9135

@jlstevens jlstevens added the type: bug Something isn't working label Jan 25, 2021
@jlstevens
Copy link
Collaborator

jlstevens commented Jan 25, 2021

Thanks for the suggestion!

I agree the order should match. I'll file this as bug though it is fairly minor as things are still understandable without the ordering be right.

@philippjfr Labelling this as a good first issue as it might be utterly trivial but depending on the internals it be the opposite. What do you think?

@jlstevens jlstevens added the good first issue Good for newcomers label Jan 25, 2021
@jlstevens jlstevens added this to the 0.7.1 milestone Jan 25, 2021
@philippjfr philippjfr modified the milestones: 0.7.1, 0.7.2, 0.8 May 21, 2021
@jlstevens jlstevens modified the milestones: 0.8, 0.81 Mar 7, 2022
@marfel
Copy link

marfel commented Mar 11, 2022

Same issue here, for an area plot. I think it would be even more useful to be able to specify the order manually. It seems to be alphabetical by default? Anyway, in my case I have one series of data (red) that produces ugly kinks in the curves, and it would be great to have the option of placing it at the top of the stacked graph.
image

@jbednar
Copy link
Member

jbednar commented Mar 11, 2022

This issue is about the legend rather than the main plot (and seems like an easy fix if someone wants to find the right spot in the code to stick [::-1]!!!). In any case, controlling the order in the main plot is definitely useful, and I would guess that it can be determined by reordering the columns in the underlying dataframe.

@marfel
Copy link

marfel commented Mar 14, 2022

Nope, there seems to be some active reordering going on. For the plot above, the columns of the DataFrame were

Alts
Ober
Unte
Bron
Volk
Wohn

@stas-sl
Copy link

stas-sl commented Mar 14, 2022

Also stumbled upon kind of related issue. Looks like you can specify order for bar chart, but for area it sorts alphabetically.

df = pd.DataFrame({'a': [2, 3, 1], 'b': [1, 2, 3], 'c': [3, 2, 1]})
df.hvplot.bar(y=['b', 'c', 'a'], stacked=True)

image

df = pd.DataFrame({'a': [2, 3, 1], 'b': [1, 2, 3], 'c': [3, 2, 1]})
df.hvplot.area(y=['b', 'c', 'a'], stacked=True)

image

@marfel
Copy link

marfel commented Mar 15, 2022

Duh. Good to know, thanks! Interestingly, the legend is still ordered exacty the other way around than the graphs.

@jbednar
Copy link
Member

jbednar commented Mar 15, 2022

That's promising; seems like flipping the legend order could be a quick fix in all these cases. I believe Bokeh determines the legend order, and it looks like Bokeh has already rejected a user request to reverse the default order, but does offer a way to reverse it: bokeh/bokeh#8901
I'd assume that somewhere in the HoloViews bokeh plotting code for charts the same fix could be applied.

As for why area charts are sorted, I briefly looked through the Area, AreaPlot, and AreaMixIn classes and couldn't find any explicit sorting; that's a mystery! I do think HoloViews shouldn't be reordering them, which would be a separate issue to open and track down.

@stas-sl
Copy link

stas-sl commented Mar 15, 2022

I do think HoloViews shouldn't be reordering them, which would be a separate issue to open and track down.

Agree, I opened it there holoviz/holoviews#5235

@philippjfr
Copy link
Member

philippjfr commented Dec 6, 2023

We can argue about whether this behavior is desirable but hvPlot being a reimplementation of the pandas .plot API one can argue that consistency with pandas behavior should be the primary concern and that is the case:

df = pd.DataFrame({'a': [2, 3, 1], 'b': [1, 2, 3], 'c': [3, 2, 1]})
df.plot.bar(y=['b', 'c', 'a'], stacked=True)

download (36)

@jbednar
Copy link
Member

jbednar commented Dec 6, 2023

Maybe the first step is to file a PR to fix that on Pandas? It's strongly desirable there as well.

@philippjfr
Copy link
Member

Just to record what I was trying (which didn't work but I think should have) and then should have allowed us to provide a fix in HoloViews:

df = pd.DataFrame({'a': [2, 3, 1], 'b': [1, 2, 3], 'c': [3, 2, 1]})

legend = []
def hook(plot, element):
    plot.handles["plot"].legend[0].items[0].label.transform = CustomJSTransform(v_func="return xs.reverse()")

df = pd.DataFrame({'a': [2, 3, 1], 'b': [1, 2, 3], 'c': [3, 2, 1]})
df.hvplot.bar(y=['b', 'c', 'a'], stacked=True).opts(hooks=[hook])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants