2D Histogram

Sleep Duration vs Quality

2D histogram correlating sleep duration with sleep quality scores.

Output
Sleep Duration vs Quality
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(42)

# Sleep data
duration = np.clip(np.random.normal(7, 1.5, 4000), 3, 12)
quality = 20 + duration * 8 + np.random.normal(0, 10, 4000)
quality = np.clip(quality, 0, 100)

fig, ax = plt.subplots(figsize=(10, 8), facecolor='#020B14')
ax.set_facecolor('#020B14')

# Custom colormap: blue to lime
colors = ['#020B14', '#0d1a3d', '#276CF5', '#6CF527']
cmap = LinearSegmentedColormap.from_list('blue_lime', colors, N=256)

h = ax.hist2d(duration, quality, bins=40, cmap=cmap, cmin=1)
cbar = plt.colorbar(h[3], ax=ax, pad=0.02)
cbar.set_label('Observations', 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('Sleep Duration (hours)', fontsize=11, color='white', fontweight='500')
ax.set_ylabel('Quality Score', fontsize=11, color='white', fontweight='500')
ax.set_title('Sleep Duration vs Quality', 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

Did this help you?

Support PyLucid to keep it free & growing

Support