5

Quick question about a HoloViews grouped (unstacked) bar chart. How can I remove the x axis variable name ticks, but have them included in a legend?

Please see example below:

barchart without legend and superfluous xticks

I would like to do the following:

  1. For the red box, remove the names for the ticks "x" and "y"
  2. Add a legend for "x" and "y" aligned to their respective colours.

Code below:

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

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

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

Thank you!

Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96
Amen_90
  • 310
  • 2
  • 9

3 Answers3

5

You can use .opts(multi_level=False) on your grouped barplot.
This will remove your x-ticks for the secondary categorical variable and add a legend to your grouped barplot.

However, you need HoloViews >=1.13 for this.
This version is, as I write this, not available yet, but you can install it via:

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


Code example:

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)


Resulting plot:

grouped barchart with legend added and xticks removed

Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96
1

As far as I searched, pandas plot with holoview (bokeh) doesn't offer styles you want. Bar chart doesn't have legend and group chart have names in x label.

Instead, use matplotlib as the backend is easy.

import pandas as pd
import numpy as np

pd.options.plotting.backend = 'matplotlib'

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

Plot = df1.plot(kind = 'bar', rot=0)
Plot

enter image description here

If you edit Bokeh directory, it offers setting legends and xlabels manually.

shimo
  • 2,156
  • 4
  • 17
  • 21
1

Question 1: removing the xticks from a grouped bar chart:

Normally you could do your_plot.opts(xticks=[(None)]) to remove xticks, but unfortunately this is not possible for grouped bar charts.

Solution to 1: render your plot as a Bokeh plot and then remove the xticks:

from bokeh.plotting import show

# create holoviews plot
my_holoviews_plot = df1.plot(kind='bar')

# render holoviews plot to bokeh plot
my_bokeh_plot = hv.render(my_holoviews_plot, backend='bokeh')

# apply changes to bokeh plot
my_bokeh_plot.xaxis.major_label_text_font_size = '0pt'

# visualize plot
show(my_bokeh_plot)



Question 2: adding a legend to a grouped bar chart:
Still looking for an answer to this one. This must also be possible using Bokeh I would think.
See my question here: https://discourse.bokeh.org/t/add-legend-afterwards-to-bokeh-plot/4519/3


(mental note for myself: you can get good insight of a lot of properties of my_bokeh_plot by pressing tab after my_bokeh_plot. in jupyter, but another good way is by running my_bokeh_plot.properties_with_values() for the more difficult properties).

Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96