Violin Plot
Global Temperature Anomalies
Decadal temperature deviation from 1951-1980 baseline with Paris Agreement target
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# Temperature anomaly data by decade
np.random.seed(42)
decades = ['1960s', '1970s', '1980s', '1990s', '2000s', '2010s', '2020s']
base_anomalies = [-0.1, 0.0, 0.15, 0.35, 0.55, 0.85, 1.1]
anomalies = [np.random.normal(base, 0.2, 120) for base in base_anomalies]
# Temperature color scale (blue cold, red warm)
def get_temp_color(val):
if val < 0: return '#3B82F6'
elif val < 0.3: return '#60A5FA'
elif val < 0.6: return '#FBBF24'
elif val < 0.9: return '#F97316'
else: return '#EF4444'
colors = [get_temp_color(b) for b in base_anomalies]
# Create figure
fig, ax = plt.subplots(figsize=(12, 7), facecolor='#F8FAFC')
ax.set_facecolor('#F8FAFC')
vp = ax.violinplot(anomalies, positions=range(len(decades)), widths=0.8,
showmeans=False, showmedians=False, showextrema=False)
for i, body in enumerate(vp['bodies']):
body.set_facecolor(colors[i])
body.set_edgecolor('white')
body.set_linewidth(2)
body.set_alpha(0.8)
# Gradient shadow
path = body.get_paths()[0]
ax.fill(path.vertices[:, 0], path.vertices[:, 1],
color=colors[i], alpha=0.2, zorder=0)
# Baseline reference
ax.axhline(0, color='#1E40AF', linewidth=2, linestyle='-', alpha=0.6)
ax.fill_between([-0.5, 6.5], -0.1, 0.1, color='#DBEAFE', alpha=0.5, zorder=0)
ax.text(6.6, 0, '1951-1980\nBaseline', fontsize=8, color='#1E40AF', va='center')
# Paris Agreement target
ax.axhline(1.5, color='#DC2626', linewidth=2, linestyle='--', alpha=0.7)
ax.text(6.6, 1.5, '+1.5°C Target', fontsize=9, color='#DC2626',
fontweight='500', va='center')
# Trend line through medians
medians = [np.median(a) for a in anomalies]
ax.plot(range(len(decades)), medians, color='#1F2937', linewidth=2.5,
linestyle='-', marker='o', markersize=8, markerfacecolor='white',
markeredgecolor='#1F2937', markeredgewidth=2, zorder=10)
# Styling
ax.set_xticks(range(len(decades)))
ax.set_xticklabels(decades, fontsize=11, fontweight='600')
ax.set_ylabel('Temperature Anomaly (°C)', fontsize=12, fontweight='500', color='#374151')
ax.set_xlim(-0.5, 6.5)
ax.set_ylim(-0.8, 2.0)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#E5E7EB')
ax.spines['bottom'].set_color('#E5E7EB')
ax.tick_params(colors='#6B7280', labelsize=10)
ax.yaxis.grid(True, linestyle='--', alpha=0.4, color='#D1D5DB')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Violin Plot examples
☕