hvplot icon indicating copy to clipboard operation
hvplot copied to clipboard

how to sort grouped bar charts

Open sophiamyang opened this issue 6 years ago • 8 comments

The grouped bar chart seems to sort alphabetically by group name, column A in my example below. However, I'd like to sort the chart by the values of column B. df.sort_values doesn't seem to work.. Could you please let me know how to sort bart charts like this? Thanks!

df = pd.DataFrame([['group B', 10, 30],['group A',20, 40],['group C',15, 10]], columns=['A','B','C']) 
df.sort_values(by='B').hvplot.bar('A',['B','C'])

sophiamyang avatar May 14 '19 16:05 sophiamyang

This appears to be a holoviews issue since the data are properly sorted in the plot.data property and holoviews creates the same plot.

Screen Shot 2019-07-22 at 10 32 27 AM

jsignell avatar Jul 22 '19 14:07 jsignell

Transferred this back to hvPlot, there is now an API to do this but it would be good to expose it in hvPlot.

philippjfr avatar Jan 15 '20 21:01 philippjfr

i actually need this and I have contributed to sorting in holoviews so I will make a PR soon.

DancingQuanta avatar Jan 22 '20 20:01 DancingQuanta

This has been fixed in HoloViews, we just need to expose options in hvPlot now.

philippjfr avatar Jan 22 '20 20:01 philippjfr

Should I make a PR or do I need to wait?

DancingQuanta avatar Jan 22 '20 20:01 DancingQuanta

I think if you install the latest versions, it's actually already working for the example you gave:

pip install git+https://github.com/holoviz/holoviews.git pip install git+https://github.com/holoviz/hvplot.git

df = pd.DataFrame([['group B', 10, 30],['group A',20, 40],['group C',15, 10]], columns=['A','B','C']) 
df.sort_values(by='B').hvplot.bar('A',['B','C'])

image

If for some reason this is not working you can also do (as a workaround):

df = pd.DataFrame([['group B', 10, 30],['group A',20, 40],['group C',15, 10]], columns=['A','B','C']) 
order = df.sort_values(by='B').A
df.hvplot.bar('A',['B','C']).redim.values(A=order)

This code also sorts on B (ascending)

SandervandenOord avatar Jan 22 '20 20:01 SandervandenOord

Actually I'm not entirely clear what the expected behavior even should be. It indeed already tries its best to respect the sort order, but for a two-category ordering this can be ambiguous. A PR to allow you to explicitly specify the categories for each level would therefore be appreciated.

philippjfr avatar Jan 22 '20 20:01 philippjfr

Here an example am working, trying to get bar chart with specific order, but not able to make it work, is there an option am missing or is related to this open issue?

df = pd.DataFrame([
    ['group B', 'EC20', 30],
    ['group B', 'EC11', 80],
    ['group B', 'EC19', 5],
    ['group A','EC20', 40],
    ['group A','EC27', 80],
    ['group A','EC22', 43],
    ['group A','EC18', 45],
    ['group A','EC24', 90.1],
    ['group A','EC25', 240.2],
    ['group A','EC26', 41],
    ['group C','EC24', 14],
    ['group C','EC15', 10],
    ['group C','EC17', 110],
    ['group C','EC18', 22],
    ['group C','EC19', 53],
], columns=['A','B','Values']) 

#not getting expected order (Chart A)
#onnly first column gets ordered
df.sort_values(by=['A','Values'],**p).plot(kind='bar',x='A',y='Values',by='B',stacked=True,height=400)

#making unique the grouping parameter 
df['A+B'] = df['A'] + '|'+ df['B']

#having expected sort but legend gets cluttered (Chart B)
df.sort_values(by=['A','Values'],**p).plot(kind='bar',x='A',y='Values',by='A+B',stacked=True,height=400)

Chart A image

Chart B image

Holoviews 1.14.1 image

lgonzalezsa avatar Feb 23 '21 21:02 lgonzalezsa