diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 8fa0f801ce9a..f8020e1fc69c 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2218,6 +2218,33 @@ def bar( x, height, width=width, bottom=bottom, align=align, **({"data": data} if data is not None else {}), **kwargs) +def grouped_bar(bar_width,df,group,categ,y): + """ + Creates a grouped bar chart given a dataframe df, column names(categ: categories column,group:column groups for each xtick ,y:height of bars) + + E.g: Consider the dataframe below + df1=pd.DataFrame({'fruit':['apple']*3+['banana']*3+['mango']*3, + 'energy':[10,20,15,10,20,15,10,20,15],'state':['notripe','ripe','mid']*3}) + + + #creates the bar plot: + + grouped_bar(bar_width=0.10,df=df1,group='state',categ='fruit',y='energy') + """ + uniq_cat=df[categ].unique() + uniq_group=df[group].unique() + + plt.figure(figsize=(7,7)) + plt.style.use('seaborn-dark-palette') + + for i,barheights in enumerate(uniq_group): + barheights=(df[df[group]==uniq_group[i]][y]).values + x=np.arange(len(uniq_cat)) + plt.bar(x+(bar_width*i),barheights,width=bar_width,label=uniq_group[i]) + + plt.xticks(ticks=x+bar_width/len(uniq_group),labels=uniq_cat) + plt.legend() + # Autogenerated by boilerplate.py. Do not edit as changes will be lost. @docstring.copy(Axes.barbs)