-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
Comments
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? |
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 |
Nope, there seems to be some active reordering going on. For the plot above, the columns of the DataFrame were
|
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) df = pd.DataFrame({'a': [2, 3, 1], 'b': [1, 2, 3], 'c': [3, 2, 1]})
df.hvplot.area(y=['b', 'c', 'a'], stacked=True) |
Duh. Good to know, thanks! Interestingly, the legend is still ordered exacty the other way around than the graphs. |
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 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. |
Agree, I opened it there holoviz/holoviews#5235 |
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) |
Maybe the first step is to file a PR to fix that on Pandas? It's strongly desirable there as well. |
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]) |
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.
This might be somehow related:
bokeh/bokeh#9135
The text was updated successfully, but these errors were encountered: