Skip to content

Instantly share code, notes, and snippets.

@naiborhujosua
Created July 26, 2022 03:38
Show Gist options
  • Save naiborhujosua/5c1c66185c19ed05ec6c825662565763 to your computer and use it in GitHub Desktop.
Save naiborhujosua/5c1c66185c19ed05ec6c825662565763 to your computer and use it in GitHub Desktop.
TIme Series Plot
# Prepare Data
data_df = pd.read_csv(DATA_DIR, encoding= 'unicode_escape', parse_dates=['Date']).head(100)
x = np.arange(data_df.shape[0])
y_returns = (data_df['Rented Bike Count'].diff().fillna(0)/data_df['Rented Bike Count'].shift(1)).fillna(0) * 100
# Plot
plt.figure(figsize=(16,10), dpi= 80)
plt.fill_between(x[1:], y_returns[1:], 0, where=y_returns[1:] >= 0, facecolor='green', interpolate=True, alpha=0.7)
plt.fill_between(x[1:], y_returns[1:], 0, where=y_returns[1:] <= 0, facecolor='red', interpolate=True, alpha=0.7)
# Annotate
plt.annotate('Peak \n Mar 2017', xy=(55.0, 280.0), xytext=(35.0, 300),
bbox=dict(boxstyle='square', fc='firebrick'),
arrowprops=dict(facecolor='steelblue', shrink=0.05), fontsize=15, color='white')
# Decorations
xtickvals = [str(m)[:3].upper()+"-"+str(y) for y,m in zip(data_df.Date.dt.year, data_df.Date.dt.month_name())]
plt.gca().set_xticks(x[::6])
plt.gca().set_xticklabels(xtickvals[::6], rotation=90, fontdict={'horizontalalignment': 'center', 'verticalalignment': 'center_baseline'})
plt.ylim(-100,350)
plt.xlim(1,100)
plt.title("Rented Bike Count Return", fontsize=22)
plt.ylabel('Monthly Rented Bike Count Return')
plt.grid(alpha=0.5)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment