Black Lives Matter. Please consider donating to Black Girls Code today.

How to plot bar plot from groupby values?

Hello Everyone

I wanted to know if there was a possibility to plot bar plots from groupby values using plotly?
Using matplotlib that’s how i did it

import matplotlib.pyplot as plt
Grouped_D = Dataset.groupby('Dept')['Weekly_Sales'].sum()
Grouped_D.plot.bar()

And that’s what i get
image

Any help would be much appreciated , thank you

Hi @Midi welcome to the forum! The easiest thing is probably to use plotly express functions since they take care of the groupby operation for you:

import plotly.express as px
df = px.data.tips()
fig = px.bar(df, x='day', y='tip')
fig.show()

or

import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x='day', y='tip', histfunc='sum')
fig.show()

(with histogram you will get one bar, where as with bar you will get one bar per line of the dataframe).
Of course you can also do the groupby yourself and then call px.par or go.Bar:

import plotly.graph_objects as go
df = px.data.tips()
dfg = df.groupby('day')['tip'].sum()
fig = px.bar(x=dfg.index, y=dfg)
# also works with graph_objects:
# fig = go.Figure(go.Bar(x=dfg.index, y=dfg))
fig.show()