1

I'm trying to add labels to a grouped hvplot barchart.

My example dataframe has the following structure:

import pandas as pd
import numpy as np
import holoviews as hv
import hvplot.pandas
hv.extension('bokeh')

df = pd.DataFrame({'A' : ['A','B','A','B','A','B'],
                   'B' : [1,1,2,2,3,3],
                   'C' : list((range(20,26)))

                  })

The bar chart is created with the following code:

bar = df.hvplot.bar(x='B', y='C', by='A')
bar

hvplot bar chart

I tried to add labels according to this and this SO questions:

labels = hv.Labels(data=df, kdims=['B','A'],vdims='C')
labels

But an overlay of both plots

bar * labels

results in an error, though the dimensions seem to be the same for me.

ValueError: all the input arrays must have same number of dimensions

:Overlay
   .Bars.I   :Bars   [B,A]   (C)
   .Labels.I :Labels   [B,A]   (C)

Any hint to the solution is appreciated. Thank you!

nsis
  • 63
  • 6

1 Answers1

0

This is possible for normal bar charts, but unfortunately this is not possible yet for grouped barcharts: https://github.com/holoviz/holoviews/pull/3385

You could create separate bar charts per category in col A and then add labels, but you won't have a grouped bar chart then:

def barplot_and_labels_category(category):
    df_subset = df[df.A == category]

    plot = df_subset.hvplot.bar(x='B', y='C', ylim=(0, 30))

    labels = hv.Labels(
        df_subset, 
        kdims=['B', 'C'], 
        vdims='C',
    ).opts(text_color='black', text_font_size='20pt')

    return plot * labels
(barplot_and_labels_category('A') + barplot_and_labels_category('B')).cols(1)
Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96
  • Thanks Sander for pointing that out! Hopefully it will be possible for bar charts soon, as this feature is highly requested in my organization. – nsis Jan 16 '20 at 12:30