2D Histogram

Correlation Density Plot

Bivariate distribution with correlation coefficient

Output
Correlation Density Plot
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# Generate correlated data
np.random.seed(42)
n = 10000

mean = [0, 0]
cov = [[1, 0.75], [0.75, 1]]
x, y = np.random.multivariate_normal(mean, cov, n).T

# Modern dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#0a0a14')
ax.set_facecolor('#0a0a14')

# Custom colormap - electric blue
colors = ['#0a0a14', '#0a1a2a', '#0a2a4a', '#1a4a7a', '#2a6aaa', 
          '#4a9ada', '#6acaff', '#8aeaff', '#aaffff', '#d0ffff', '#ffffff']
cmap = LinearSegmentedColormap.from_list('electric', colors, N=256)

# 2D histogram
h = ax.hist2d(x, y, bins=60, cmap=cmap, cmin=1)

# Correlation ellipse approximation
theta = np.linspace(0, 2*np.pi, 100)
r = 2
ellipse_x = r * np.cos(theta)
ellipse_y = r * (0.75 * np.cos(theta) + 0.66 * np.sin(theta))
ax.plot(ellipse_x, ellipse_y, '--', color='#6acaff', linewidth=2, alpha=0.6, label='2σ Ellipse')

# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#0a0a14')
cbar.set_label('Density', fontsize=11, color='#a0d0f0', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#a0d0f0')
cbar.outline.set_edgecolor('#2a3a5a')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#a0d0f0', fontsize=9)

# Labels
ax.set_xlabel('Variable X', fontsize=13, color='#a0d0f0', fontweight='600', labelpad=10)
ax.set_ylabel('Variable Y', fontsize=13, color='#a0d0f0', fontweight='600', labelpad=10)
ax.set_title('Bivariate Correlation Density', fontsize=16, color='white', fontweight='bold', pad=20)

ax.tick_params(colors='#80b0d0', labelsize=10, length=0)
for spine in ax.spines.values():
    spine.set_visible(False)

ax.legend(loc='upper left', fontsize=9, facecolor='#1a2a3a', edgecolor='#2a4a6a', labelcolor='#a0d0f0')
ax.set_aspect('equal')
plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support