Created
January 25, 2023 07:52
-
-
Save muhammadanas0716/a530cd5e4fd7acab84efe4c9a53193a3 to your computer and use it in GitHub Desktop.
By coloring the area between the axis and the lines, the area chart throws more emphasis not just on the peaks and troughs but also the duration of the highs and lows. The longer the duration of the highs, the larger is the area under the line.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import needed libs | |
import numpy as np | |
import pandas as pd | |
import matplotlib as mpl | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import numpy as np | |
import pandas as pd | |
# Prepare Data | |
df = pd.read_csv("https://github.com/selva86/datasets/raw/master/economics.csv", parse_dates=['date']).head(100) | |
x = np.arange(df.shape[0]) | |
y_returns = (df.psavert.diff().fillna(0)/df.psavert.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 \n1975', xy=(94.0, 21.0), xytext=(88.0, 28), | |
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(df.date.dt.year, 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(-35,35) | |
plt.xlim(1,100) | |
plt.title("Month Economics Return %", fontsize=22) | |
plt.ylabel('Monthly returns %') | |
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