2D Histogram
Protein vs Calorie Intake
2D histogram of daily protein versus calorie consumption patterns.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
np.random.seed(42)
# Nutrition data
calories = np.concatenate([
np.random.normal(1800, 300, 2000),
np.random.normal(2500, 400, 2500),
np.random.normal(3200, 350, 1000)
])
protein = calories * 0.04 + np.random.normal(0, 15, len(calories))
protein = np.clip(protein, 20, 200)
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#020B14')
ax.set_facecolor('#020B14')
# Custom colormap: amber to coral
colors = ['#020B14', '#2d1a0d', '#F5B027', '#F5276C']
cmap = LinearSegmentedColormap.from_list('amber_coral', colors, N=256)
h = ax.hist2d(calories, protein, bins=45, cmap=cmap, cmin=1)
cbar = plt.colorbar(h[3], ax=ax, pad=0.02)
cbar.set_label('Records', 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('Daily Calories', fontsize=11, color='white', fontweight='500')
ax.set_ylabel('Protein (g)', fontsize=11, color='white', fontweight='500')
ax.set_title('Protein vs Calorie Intake', 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
☕