How to create an Area Chart on Python in 3 screenshots
2 min readJul 11, 2021
- Showing the rise and fall of various data series over time.
- Conveying total amounts over time as well as some sub-categorical breakdowns (but only to a point).
- Emphasizing a part-to-whole relationship over time when one part is very large, or changes from being very large to very small.
- Showing change over time.
Screenshot 1 — That your data:
## reference ##
## https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/#44.-Area-Chart-UnStacked
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.DataFrame({
'Goblins': [45, 90, 65, 48, 100, 48, 60],
'Knights': [156, 140, 125, 90, 40, 12, 5],
'Soccer Players': [10, 15, 25, 56, 70, 140, 200],
}, index=[2015, 2016, 2017, 2018, 2019, 2020, 2021])
df.head()
Screenshot 2 — That your actions:
ax = df.plot.area(stacked=False, figsize=(10, 6))
Screenshot 3 — Make it look better:
x = df.index.values.tolist()
y1 = df['Goblins'].values.tolist()
y2 = df['Knights'].values.tolist()
y3 = df['Soccer Players'].values.tolist()
columns = ['Goblins', 'Knights', 'Soccer Players']fig, ax = plt.subplots(1, 1, figsize=(12,8), dpi= 80)
ax.fill_between(x, y1=y1, y2=0, label='Goblins', alpha=0.5, color='brown', linewidth=2)
ax.fill_between(x, y1=y2, y2=0, label='Knights', alpha=0.5, color='blue', linewidth=2)
ax.fill_between(x, y1=y3, y2=0, label='Soccer Players', alpha=0.5, color='grey', linewidth=2)
# Decorations
ax.set_title('Performance across time', fontsize=16, color='grey')
ax.legend(fontsize=12)
# Lighten borders
plt.gca().spines["top"].set_alpha(0)
plt.gca().spines["bottom"].set_alpha(.3)
plt.gca().spines["right"].set_alpha(0)
plt.gca().spines["left"].set_alpha(.3)
plt.show()
For more visualization and analytics
https://drivenn.io/