2D Histogram
CO2 vs Temperature Anomaly
Climate science visualization showing the correlation between atmospheric CO2 levels and global temperature anomalies.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
# Climate data
np.random.seed(42)
n_measurements = 6000
co2 = np.random.uniform(300, 420, n_measurements)
temp_anomaly = 0.018 * (co2 - 280) + np.random.normal(0, 0.25, n_measurements)
temp_anomaly += np.random.uniform(-0.3, 0.3, n_measurements)
# Climate dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#0a1015')
ax.set_facecolor('#0a1015')
# Custom colormap - warming
colors = ['#0a1015', '#0a2028', '#1a4040', '#2a6050', '#4a8060',
'#6aa070', '#8ac080', '#b0e090', '#d0f0a0', '#f0ffb0', '#ffffc0']
cmap = LinearSegmentedColormap.from_list('warming', colors, N=256)
# 2D histogram
h = ax.hist2d(co2, temp_anomaly, bins=50, cmap=cmap, cmin=1)
# Climate thresholds
ax.axhline(y=0, color='#8ac080', linestyle='--', alpha=0.6, linewidth=1.5, label='Pre-industrial')
ax.axhline(y=1.5, color='#ffa502', linestyle=':', alpha=0.7, linewidth=2, label='1.5C Paris Target')
ax.axhline(y=2.0, color='#ff6b6b', linestyle='--', alpha=0.7, linewidth=2, label='2.0C Threshold')
# Trend line
z = np.polyfit(co2, temp_anomaly, 1)
p = np.poly1d(z)
x_line = np.linspace(300, 420, 100)
ax.plot(x_line, p(x_line), '-', color='#d0f0a0', linewidth=2.5, alpha=0.8)
# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#0a1015')
cbar.set_label('Measurement Density', fontsize=11, color='#b0e0a0', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#b0e0a0')
cbar.outline.set_edgecolor('#2a4035')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#b0e0a0', fontsize=9)
ax.set_xlabel('Atmospheric CO2 (ppm)', fontsize=13, color='#b0e0a0', fontweight='600', labelpad=10)
ax.set_ylabel('Temperature Anomaly (C)', fontsize=13, color='#b0e0a0', fontweight='600', labelpad=10)
ax.set_title('CO2 vs Global Temperature Anomaly', fontsize=16, color='white', fontweight='bold', pad=20)
ax.tick_params(colors='#90c080', labelsize=10, length=0)
for spine in ax.spines.values():
spine.set_visible(False)
ax.legend(loc='upper left', fontsize=9, facecolor='#1a2825', edgecolor='#2a4035', labelcolor='#b0e0a0')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More 2D Histogram examples
☕