Area Chart
PCA Variance Explained
Cumulative variance explained with scree plot - R style
Output
Python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
n_components = 10
variance_ratio = np.array([0.35, 0.20, 0.12, 0.10, 0.08, 0.06, 0.04, 0.03, 0.015, 0.005])
cumulative = np.cumsum(variance_ratio)
fig, ax = plt.subplots(figsize=(10, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
x = np.arange(1, n_components + 1)
# Cumulative area
ax.fill_between(x, 0, cumulative, alpha=0.3, color='#4927F5', step='mid')
ax.step(x, cumulative, color='#4927F5', linewidth=2.5, where='mid', label='Cumulative')
# Individual bars
ax.bar(x, variance_ratio, color='#27D3F5', alpha=0.8, width=0.6, label='Individual')
# Threshold lines
ax.axhline(0.8, color='#F5276C', linewidth=1.5, linestyle='--', label='80% threshold')
ax.axhline(0.9, color='#F5B027', linewidth=1, linestyle=':', alpha=0.7)
# Mark elbow
ax.scatter([3], [cumulative[2]], color='#F5276C', s=100, zorder=5, edgecolors='white', linewidth=2)
ax.annotate('Elbow (3 PCs)', (3, cumulative[2]), xytext=(15, -20), textcoords='offset points',
color='#F5276C', fontsize=10, fontweight='bold',
arrowprops=dict(arrowstyle='->', color='#F5276C'))
ax.set_xlabel('Principal Component', color='#1f2937', fontsize=11, fontweight='500')
ax.set_ylabel('Proportion of Variance', color='#1f2937', fontsize=11, fontweight='500')
ax.set_title('PCA Variance Explained', color='#1f2937', fontsize=13, fontweight='600', pad=15)
ax.tick_params(colors='#374151', labelsize=9)
ax.set_xticks(x)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#e5e7eb')
ax.spines['bottom'].set_color('#e5e7eb')
ax.yaxis.grid(True, color='#f3f4f6', linewidth=0.8)
ax.set_ylim(0, 1.05)
ax.legend(facecolor='#ffffff', edgecolor='#e5e7eb', labelcolor='#1f2937', loc='center right')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Basic Charts
More Area Chart examples
☕