Skip to content

Instantly share code, notes, and snippets.

@yosukesan
Last active February 24, 2023 06:36
Show Gist options
  • Save yosukesan/74bcc9028860254f6dd1b4bf8f774bac to your computer and use it in GitHub Desktop.
Save yosukesan/74bcc9028860254f6dd1b4bf8f774bac to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
df = pd.DataFrame(columns=['Sales'],
index=[dt.datetime(2022,1,1), dt.datetime(2022,1,2), dt.datetime(2022,1,3), dt.datetime(2022,1,4)])
df['Sales'] = [100,200,500,-100]
df['Change in %'] = df.pct_change()
#df = df.fillna(0)
print(df)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.xticks(rotation=60)
ax.bar(df.index, df['Sales'], alpha=0.5)
ax.set_ylabel('Sales')
ax2 = ax.twinx()
ax2.plot(df['Change in %'], 'o-', color='red')
for x, y in zip(df.index, df['Change in %']):
ax2.text(x, y+y*0.1, y)
for x, y in zip(df.index, df['Sales']):
ax.text(x-dt.timedelta(hours=8), y-y*0.3, y, color='white')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment