Hello Everyone
How to create a grouped bar chart on a data frame?
I want to show the count of each violation distributed by gender i.e 2 bars(male and female) for each violation. Can anyone help me how to do it?
Thanks
Santosh
Hello Everyone
How to create a grouped bar chart on a data frame?
I want to show the count of each violation distributed by gender i.e 2 bars(male and female) for each violation. Can anyone help me how to do it?
Thanks
Santosh
Hello Santosh!
Dash and Plotly, unlike seaborn and matplotlib, do not operate on the data. you will have to process the data prior to supplying it as ‘x’ and ‘y’ values to the plot. You will have to plot two plots- one for Male and one for female, as follows:
df2= df_g.groupby(['violation','driver_gender']).size().unstack(fill_value=0).stack()
x_uni=df_g['driver_gender'].unique()
y_uni=df_g['violation'].unique()
for i in range(len(x_uni)):
for j in range(len(y_uni)):
datalist=[]
for k in range(nx):
datalist.append(df2[x_uni[i]][y_uni[j]])
trace= go.Bar(
x= x_uni,
y=datalist,
visible=True,
name=y_label[j],
marker=dict(color=colors[j]),
)
DataOut.append(trace)
This may initially seem a little complicated but this works for as many categories as you like.
Hope this helped!
Santoshi
Hello Santoshi,
Thank you so much for helping me. I’m getting an error regarding nx…which is not defined anywhere else.
Yes looks little complicated but if it works then it will be great!
Thanks
Santosh
for i in range(len(x_uni)):
datalist=[]
for j in range(len(y_uni)):
datalist.append(df2[x_uni[i]][y_uni[j]])
trace= go.Bar(
x= x_uni,
y=datalist,
visible=True,
name=y_label[j],
marker=dict(color=colors[j]),
)
DataOut.append(trace)
This should be fine.
The previous code was for a dropdown of multiple plots. my bad.
Hope this helps!