Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down