2D Histogram
Temperature vs Humidity
Weather observation density with comfort zone
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
# Weather station data
np.random.seed(42)
n_readings = 10000
# Temperature and humidity with seasonal patterns
temp = np.random.normal(22, 10, n_readings)
temp = np.clip(temp, -5, 42)
# Humidity inversely related to high temps
humidity = 70 - 0.8 * temp + np.random.normal(0, 15, n_readings)
humidity = np.clip(humidity, 15, 100)
# Weather dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#0a1018')
ax.set_facecolor('#0a1018')
# Custom colormap - weather blue to warm
colors = ['#0a1018', '#0a2030', '#1a4050', '#2a6070', '#4a8090',
'#6aa0a5', '#90c0b0', '#b0e0c0', '#d0f0d0', '#f0ffe0', '#ffffb0']
cmap = LinearSegmentedColormap.from_list('weather', colors, N=256)
# 2D histogram
h = ax.hist2d(temp, humidity, bins=50, cmap=cmap, cmin=1)
# Comfort zones
from matplotlib.patches import Rectangle
comfort = Rectangle((18, 40), 10, 25, fill=False, edgecolor='#90c0b0',
linewidth=2.5, linestyle='-', alpha=0.8, label='Comfort Zone')
ax.add_patch(comfort)
# Dew point approximation line
temp_line = np.linspace(-5, 42, 100)
dew_line = 100 - 2.5 * temp_line
ax.plot(temp_line, dew_line, '--', color='#6aa0a5', linewidth=1.5, alpha=0.6, label='High Dew Point')
# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#0a1018')
cbar.set_label('Reading Count', fontsize=11, color='#b0d0c0', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#b0d0c0')
cbar.outline.set_edgecolor('#2a4048')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#b0d0c0', fontsize=9)
# Labels
ax.set_xlabel('Temperature (°C)', fontsize=13, color='#b0d0c0', fontweight='600', labelpad=10)
ax.set_ylabel('Relative Humidity (%)', fontsize=13, color='#b0d0c0', fontweight='600', labelpad=10)
ax.set_title('Temperature vs Humidity Distribution', fontsize=16, color='white',
fontweight='bold', pad=20)
# Style axes
ax.tick_params(colors='#90b0a0', labelsize=10, length=0)
for spine in ax.spines.values():
spine.set_visible(False)
ax.legend(loc='upper right', fontsize=9, facecolor='#1a2a30', edgecolor='#2a4048',
labelcolor='#b0d0c0', framealpha=0.9)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More 2D Histogram examples
☕