Skip to content

Instantly share code, notes, and snippets.

@naiborhujosua
Created July 26, 2022 03:39
Show Gist options
  • Save naiborhujosua/f0cc4b68c2a46a374dd3a0f26a743c6c to your computer and use it in GitHub Desktop.
Save naiborhujosua/f0cc4b68c2a46a374dd3a0f26a743c6c to your computer and use it in GitHub Desktop.
Order Bar Chart
# Prepare Data
df = data[['Rented Bike Count', 'Seasons']].groupby('Seasons').apply(lambda x: x.mean())
df.sort_values('Rented Bike Count', inplace=True)
df.reset_index(inplace=True)
# Draw plot
import matplotlib.patches as patches
fig, ax = plt.subplots(figsize=(16,10), facecolor='white', dpi= 80)
ax.vlines(x=df.index, ymin=0, ymax=df['Rented Bike Count'], color='firebrick', alpha=0.7, linewidth=20)
# Annotate Text
for i, rbc in enumerate(df['Rented Bike Count']):
ax.text(i, rbc+0.3, round(rbc, 1), horizontalalignment='center', fontsize=16)
# Title, Label, Ticks and Ylim
ax.set_title('Bar Chart for Rented Bike Count', fontdict={'size':22})
ax.set(ylabel='Rented Bike Count', ylim=(0, 1500))
plt.xticks(df.index, df.Seasons.str.upper(), rotation=60, horizontalalignment='right', fontsize=12)
# Add patches to color the X axis labels
p1 = patches.Rectangle((.57, -0.005), width=.33, height=.13, alpha=.1, facecolor='green', transform=fig.transFigure)
p2 = patches.Rectangle((.124, -0.005), width=.446, height=.13, alpha=.1, facecolor='red', transform=fig.transFigure)
fig.add_artist(p1)
fig.add_artist(p2)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment