Search code examples
pythonholoviews

Holoviews: HLine error when trying to plot on categorical groupby bar plot


Just trying to add a HLine (which is a mean of the values) to a groupby barplot I have created, however, I keep getting a value error "all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)". Anyone know where I could be going wrong? Data example that produces the error below:

Data_test = [['2010', 5, 'Yes'], ['2010', 7, 'No'], 
         ['2011', 3, 'Yes'], ['2011', 5, 'No'], 
         ['2012', 7, 'Yes'], ['2012', 3, 'No'],
         ['2013', 8, 'Yes'], ['2013', 7, 'No'],
         ['2014', 2, 'Yes'], ['2014', 3, 'No'],
         ['2015', 6, 'Yes'], ['2015', 7, 'No'],
         ['2016', 1, 'Yes'], ['2016', 7, 'No'],
         ['2017', 9, 'Yes'], ['2017', 3, 'No'],
         ['2018', 7, 'Yes'], ['2018', 5, 'No'],
         ['2019', 3, 'Yes'], ['2019', 9, 'No']]

test_df = pd.DataFrame(Data_test, columns = ['Year', 'Value', 'Category'])
test_plot = test_df.groupby(['Year', 'Category'])['Value'].mean().plot(kind = 'bar').opts(multi_level = False) * hv.HLine(test_df['Value'].mean())

test_plot

Thanks for any suggestions!


Solution

  • It is not clear what error you are seeing. If I run the code you have posted, it works, if you override the default plot method of Series object with hvplot. The line for plotting should be

    test_plot = test_df.groupby(['Year', 'Category'])['Value'].mean().hvplot.bar().options(multi_level = False) * hv.HLine(test_df['Value'].mean())
    

    And you get the plot.
    The remaining code is same as yours

    import pandas as pd
    import holoviews as hv
    import hvplot.pandas
    
    Data_test = [['2010', 5, 'Yes'], ['2010', 7, 'No'], 
             ['2011', 3, 'Yes'], ['2011', 5, 'No'], 
             ['2012', 7, 'Yes'], ['2012', 3, 'No'],
             ['2013', 8, 'Yes'], ['2013', 7, 'No'],
             ['2014', 2, 'Yes'], ['2014', 3, 'No'],
             ['2015', 6, 'Yes'], ['2015', 7, 'No'],
             ['2016', 1, 'Yes'], ['2016', 7, 'No'],
             ['2017', 9, 'Yes'], ['2017', 3, 'No'],
             ['2018', 7, 'Yes'], ['2018', 5, 'No'],
             ['2019', 3, 'Yes'], ['2019', 9, 'No']]
    
    test_df = pd.DataFrame(Data_test, columns = ['Year', 'Value', 'Category'])
    test_plot = test_df.groupby(['Year', 'Category'])['Value'].mean().hvplot.bar().options(multi_level = False) * hv.HLine(test_df['Value'].mean())
    test_plot
    

    enter image description here