Grouped barchart: add legend and remove xticks

Basically I was trying to solve this stackoverflow question on grouped barcharts.
The goal is to add a legend to a grouped barchart and remove xticks.
https://stackoverflow.com/questions/59679386/holoviews-remove-variables-names-from-x-axis-on-bar-chart

I gave it a go, but I don’t know how I can add a legend to a grouped barchart. I tried legend=True, but that didn’t work.
I saw this github issue: https://github.com/holoviz/holoviews/issues/3710
This says for a grouped barchart, it is hardcoded that there is no legend. So how can a legend be added? Maybe by getting the bokeh figure?

Then about trying to remove the xticks from the grouped barchart.
I was also unsuccessful. I found this:
https://github.com/holoviz/holoviews/issues/2640
Which comes down to: hv.Curve(range(11)).options(xticks=[(0, 'zero'), (5, 'five'), (10, 'ten')])
But .opts(xticks=[(0, 0)]) doesn’t work for grouped barcharts.
Is there a way to get rid of xticks with (grouped) barcharts?

I understand that you sometimes just want a legend instead of extra xticks to explain your categories.

1 Like

This has been addressed in https://github.com/holoviz/holoviews/pull/4183

2 Likes

You can find a solution here:
https://stackoverflow.com/questions/59679386/holoviews-remove-variables-names-from-x-axis-on-grouped-bar-chart

Use .opts(multi_level=False):

import numpy as np
import pandas as pd
import holoviews as hv
pd.options.plotting.backend = 'holoviews'

df1 = pd.DataFrame({
    'x': np.random.rand(10), 
    'y': np.random.rand(10),
})

my_grouped_barplot = df1.plot(kind='bar')

# remove the 2nd categorical level with multi_level=False
# this will remove your x-ticks and add a legend
my_grouped_barplot.opts(multi_level=False)
1 Like

Thanks! I have been looking all over for this.

It should be added to the documentation here: Bar — hvPlot 0.9.0 documentation

Perhaps relevant to Bars plot, x label (two levels) rotation