Hexbin Plot
Energy Consumption Pattern
Temperature vs electricity usage for smart grid optimization.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
np.random.seed(42)
n_readings = 12000
temperature = np.random.normal(20, 12, n_readings)
temperature = np.clip(temperature, -10, 45)
base_usage = 100 + 5 * np.abs(temperature - 18)
usage = base_usage + np.random.exponential(20, n_readings)
usage = np.clip(usage, 50, 400)
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#ffffff')
ax.set_facecolor('#fff7ed')
colors = ['#fff7ed', '#ffedd5', '#fed7aa', '#fdba74', '#fb923c',
'#f97316', '#ea580c', '#c2410c', '#9a3412', '#7c2d12']
cmap = LinearSegmentedColormap.from_list('orange', colors, N=256)
hb = ax.hexbin(temperature, usage, gridsize=35, cmap=cmap, mincnt=1,
edgecolors='white', linewidths=0.3)
ax.axvline(x=18, color='#16a34a', linestyle='-', alpha=0.7, linewidth=2, label='Optimal Temp (18C)')
from matplotlib.patches import Rectangle
comfort = Rectangle((15, 50), 10, 100, fill=False, edgecolor='#16a34a',
linewidth=2, linestyle='-', alpha=0.8, label='Comfort Zone')
ax.add_patch(comfort)
temp_line = np.linspace(-10, 45, 100)
usage_line = 100 + 5 * np.abs(temp_line - 18)
ax.plot(temp_line, usage_line, '-', color='#9a3412', linewidth=2, alpha=0.7, label='Baseline')
cbar = plt.colorbar(hb, ax=ax, pad=0.02, shrink=0.85)
cbar.set_label('Reading Count', fontsize=11, color='#7c2d12', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#9a3412')
cbar.outline.set_edgecolor('#fed7aa')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#9a3412', fontsize=9)
ax.set_xlabel('Temperature (C)', fontsize=12, color='#7c2d12', fontweight='600', labelpad=12)
ax.set_ylabel('Energy Usage (kWh)', fontsize=12, color='#7c2d12', fontweight='600', labelpad=12)
ax.set_title('Energy Consumption vs Temperature', fontsize=16, color='#431407', fontweight='700', pad=20)
ax.tick_params(colors='#9a3412', labelsize=10, length=0)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#fed7aa')
ax.spines['bottom'].set_color('#fed7aa')
ax.legend(loc='upper right', fontsize=9, frameon=True, facecolor='white',
edgecolor='#fed7aa', labelcolor='#7c2d12')
ax.grid(True, alpha=0.3, color='#fed7aa', linestyle='-', linewidth=0.5)
ax.set_axisbelow(True)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Hexbin Plot examples
☕