JoshK
JoshK

Reputation: 89

Hvplot/bokeh summed Bar chart from Pandas Dataframe

I'm trying to print a "simple" Bar chart, using HVPlot and bokeh in jupyter notebook. Here is some simplified data:

My Data originally looks like this:

My goal is to get a bar chart like That (Note it doesn't have to be stacked. The only importatnt thing are the Totals.):

Since I couldn't figure out how to get a bar chart with the sum of certain columns, I used pandas.melt to model the Data to look like that:

With this Data I can plot it, but then the values aren't summed. Instead, there are multiple Bars behind each other.

Here is the code I used to test:

 testd = {'Name': ['Item1', 'Item2','Item3','Item3'],'Filter': ['F1','F2','F1','F1'], 
                 'Count': [1,5,2,1], 'CountCategory': ['CountA','CountB','CountA','CountD']}
 testdf = pd.DataFrame(data=testd)
 testdf.hvplot.bar('CountCategory','Count',groupby='Filter', rot=90, aggregator=np.sum)

It doesn't change anything if I omit the aggregator=np.sum

Does anyone know how to properly plot this? It doesn't have to use the "transposed" data since I'm only doing that because I have no idea how to plot the Original Data. And another question would be if there is a possibility

Upvotes: 2

Views: 1851

Answers (1)

philippjfr
philippjfr

Reputation: 4080

The aggregator is used by the datashade/rasterize operation to aggregate the data and indeed has no effect on bar plots. If you want to aggregate the data I recommend doing so using pandas methods. However in your case I don't think that's the issue, the main problem in implementing the plot you requested is that in holoviews the legend is generally linked to the styling, which means that you can't easily get the legend to display the filter and color each bar separately.

You could do this and add the Filter as a hover column, which means you still have access to it:

testdf.hvplot.bar('CountCategory', 'Count', by='Name', stacked=True, rot=90, hover_cols=['Filter'])

barplot1

I'll probably raise an issue in HoloViews to support a legend decoupled from the styling.

Upvotes: 2

Related Questions