Skip to content

Instantly share code, notes, and snippets.

@naiborhujosua
Created July 26, 2022 03:44
Show Gist options
  • Save naiborhujosua/c0b136a445240ad5394660c0858fd525 to your computer and use it in GitHub Desktop.
Save naiborhujosua/c0b136a445240ad5394660c0858fd525 to your computer and use it in GitHub Desktop.
Box Plot
# Draw Plot
plt.figure(figsize=(13,10), dpi= 80)
sns.boxplot(x='Seasons', y='Rented Bike Count', data=data, notch=False)
# Add N Obs inside boxplot (optional)
def add_n_obs(df,group_col,y):
medians_dict = {grp[0]:grp[1][y].median() for grp in df.groupby(group_col)}
xticklabels = [x.get_text() for x in plt.gca().get_xticklabels()]
n_obs = df.groupby(group_col)[y].size().values
for (x, xticklabel), n_ob in zip(enumerate(xticklabels), n_obs):
plt.text(x, medians_dict[xticklabel]*1.01, "#obs : "+str(n_ob), horizontalalignment='center', fontdict={'size':14}, color='white')
add_n_obs(data,group_col='Seasons',y='Rented Bike Count')
# Decoration
plt.title('Box Plot of Highway Mileage by Vehicle Class', fontsize=22)
plt.ylim(-400, 3700)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment