Contour Plot
Brain fMRI Activation
Neuroscience visualization of cortical activation patterns during cognitive tasks.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
# Create brain-shaped grid
x = np.linspace(-5, 5, 200)
y = np.linspace(-4, 4, 160)
X, Y = np.meshgrid(x, y)
# Brain boundary (ellipse)
a, b = 4.5, 3.5
brain_mask = (X/a)**2 + (Y/b)**2 <= 1
# Activation patterns
motor = 3 * np.exp(-((X-0)**2/2 + (Y-2.5)**2/1))
visual = 4 * np.exp(-((X-3)**2/1 + (Y-0)**2/2))
prefrontal = 2.5 * np.exp(-((X+3)**2/2 + (Y+1)**2/1.5))
language = 3.5 * np.exp(-((X+2)**2/1 + (Y-1)**2/1))
Z = motor + visual + prefrontal + language
Z = Z * brain_mask
Z[~brain_mask] = np.nan
# Neuroscience dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#080810')
ax.set_facecolor('#080810')
# Custom colormap - brain activity cyan-orange
colors = ['#080810', '#0a1020', '#0a2040', '#1a4060', '#2a6080',
'#4a90a0', '#6ac0c0', '#90f0e0', '#c0fff0', '#ffffff']
cmap = LinearSegmentedColormap.from_list('brain', colors, N=256)
# Filled contour
cf = ax.contourf(X, Y, Z, levels=22, cmap=cmap)
# Brain outline with glow
brain_theta = np.linspace(0, 2*np.pi, 100)
brain_x = a * np.cos(brain_theta)
brain_y = b * np.sin(brain_theta)
ax.plot(brain_x, brain_y, color='#6ac0c0', linewidth=3, alpha=0.8)
ax.plot(brain_x, brain_y, color='#6ac0c0', linewidth=6, alpha=0.2)
# Central sulcus
ax.plot([0, 0], [-3, 3], '--', color='#4a90a0', linewidth=2, alpha=0.6)
# Region labels with backgrounds
regions = [('Motor', 0, 2.8), ('Visual', 3.2, 0), ('Prefrontal', -3.2, -0.8), ('Language', -2, 1.5)]
for name, rx, ry in regions:
ax.text(rx, ry, name, fontsize=10, color='white', ha='center', fontweight='500',
bbox=dict(boxstyle='round,pad=0.3', facecolor='#080810', edgecolor='#4a90a0', alpha=0.8))
# Styled colorbar
cbar = plt.colorbar(cf, ax=ax, pad=0.02, shrink=0.8)
cbar.ax.set_facecolor('#080810')
cbar.set_label('BOLD Signal Intensity', fontsize=11, color='#c0e0f0', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#c0e0f0')
cbar.outline.set_edgecolor('#2a4050')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#c0e0f0', fontsize=9)
# Labels
ax.set_xlabel('Lateral Position (cm)', fontsize=13, color='#c0e0f0', fontweight='600', labelpad=10)
ax.set_ylabel('Anterior-Posterior (cm)', fontsize=13, color='#c0e0f0', fontweight='600', labelpad=10)
ax.set_title('fMRI Cortical Activation Map', fontsize=16, color='white',
fontweight='bold', pad=20)
# Style axes
ax.tick_params(colors='#80b0c0', labelsize=10, length=0)
for spine in ax.spines.values():
spine.set_visible(False)
ax.set_aspect('equal')
ax.set_xlim(-5.5, 5.5)
ax.set_ylim(-4.5, 4.5)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Contour Plot examples
☕