Hexbin Plot

Bivariate Density Analysis

Hexbin visualization of bivariate normal distribution with correlation

Output
Bivariate Density Analysis
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(42)
x = np.random.randn(8000)
y = 0.8 * x + np.random.randn(8000) / 2

fig, ax = plt.subplots(figsize=(10, 8), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')

colors = ['#0a0a0f', '#1a1a3a', '#2d1f5a', '#4a1a7a', '#6b2d9a', '#8b3fba', '#ab5fda', '#cb8ffa']
cmap = LinearSegmentedColormap.from_list('purple_glow', colors, N=256)

hb = ax.hexbin(x, y, gridsize=30, cmap=cmap, mincnt=1, edgecolors='none')

cbar = plt.colorbar(hb, ax=ax, pad=0.02, shrink=0.8)
cbar.ax.set_facecolor('#0a0a0f')
cbar.outline.set_edgecolor('#2a2a4a')
cbar.ax.tick_params(colors='#8b8fba', labelsize=9)
cbar.set_label('Count', color='#8b8fba', fontsize=10)

ax.set_xlabel('X Variable', color='#8b8fba', fontsize=11)
ax.set_ylabel('Y Variable', color='#8b8fba', fontsize=11)
ax.tick_params(colors='#8b8fba', labelsize=10)
for spine in ax.spines.values():
    spine.set_visible(False)

plt.tight_layout()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support