2D Histogram
Exam Score vs Study Hours
2D histogram of student exam scores versus hours of study.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
np.random.seed(42)
# Education data
study_hours = np.clip(np.random.exponential(10, 4000), 0, 50)
score = 30 + study_hours * 1.5 + np.random.normal(0, 10, 4000)
score = np.clip(score, 0, 100)
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#020B14')
ax.set_facecolor('#020B14')
# Custom colormap: deep_purple to cyan
colors = ['#020B14', '#1a0d3d', '#5314E6', '#27D3F5']
cmap = LinearSegmentedColormap.from_list('purple_cyan', colors, N=256)
h = ax.hist2d(study_hours, score, bins=40, cmap=cmap, cmin=1)
cbar = plt.colorbar(h[3], ax=ax, pad=0.02)
cbar.set_label('Students', color='white', fontsize=11)
cbar.ax.yaxis.set_tick_params(color='white')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='white')
ax.set_xlabel('Study Hours', fontsize=11, color='white', fontweight='500')
ax.set_ylabel('Exam Score', fontsize=11, color='white', fontweight='500')
ax.set_title('Exam Score vs Study Hours', fontsize=14, color='white', fontweight='bold', pad=15)
ax.tick_params(colors='white', labelsize=9)
for spine in ax.spines.values():
spine.set_color('#333333')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More 2D Histogram examples
☕