2D Histogram
Earthquake Magnitude vs Depth
Seismic event distribution with danger threshold
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
# Seismic data
np.random.seed(42)
n_quakes = 8000
# Magnitude (Gutenberg-Richter law approximation)
magnitude = np.random.exponential(0.8, n_quakes) + 2
magnitude = np.clip(magnitude, 2, 9)
# Depth (km) - bimodal for shallow and deep
shallow = np.random.exponential(30, n_quakes // 2)
deep = np.random.normal(300, 100, n_quakes // 2)
depth = np.concatenate([shallow, deep])
depth = np.clip(depth, 0, 700)
# Seismic dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#0c0808')
ax.set_facecolor('#0c0808')
# Custom colormap - seismic red-orange
colors = ['#0c0808', '#1c0c0c', '#2c1010', '#4c1818', '#6c2020',
'#8c3030', '#ac4040', '#cc5050', '#ec7060', '#ff9080', '#ffb0a0']
cmap = LinearSegmentedColormap.from_list('seismic', colors, N=256)
# 2D histogram
h = ax.hist2d(magnitude, depth, bins=[40, 50], cmap=cmap, cmin=1)
# Depth zones
ax.axhline(y=70, color='#ff9080', linestyle='--', alpha=0.6, linewidth=1.5, label='Shallow/Intermediate')
ax.axhline(y=300, color='#cc5050', linestyle=':', alpha=0.6, linewidth=1.5, label='Intermediate/Deep')
# Major quake line
ax.axvline(x=7, color='#ffb0a0', linestyle='--', alpha=0.5, linewidth=1.5, label='Major (M7+)')
# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#0c0808')
cbar.set_label('Event Count', fontsize=11, color='#e0a090', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#e0a090')
cbar.outline.set_edgecolor('#3c2020')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#e0a090', fontsize=9)
# Labels
ax.set_xlabel('Magnitude', fontsize=13, color='#e0a090', fontweight='600', labelpad=10)
ax.set_ylabel('Depth (km)', fontsize=13, color='#e0a090', fontweight='600', labelpad=10)
ax.set_title('Earthquake Distribution by Magnitude & Depth', fontsize=16, color='white', fontweight='bold', pad=20)
ax.tick_params(colors='#c08070', labelsize=10, length=0)
for spine in ax.spines.values():
spine.set_visible(False)
ax.legend(loc='upper right', fontsize=9, facecolor='#1c1010', edgecolor='#3c2020', labelcolor='#e0a090')
ax.invert_yaxis()
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More 2D Histogram examples
☕