2D Histogram

Trading Volume vs Price Change

Financial analysis showing relationship between trading volume and price movements in high-frequency data.

Output
Trading Volume vs Price Change
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# Trading data
np.random.seed(42)
n_trades = 15000

volume = np.random.lognormal(10, 1.5, n_trades) / 1000
base_change = np.random.laplace(0, 0.5, n_trades)
volume_effect = np.sign(np.random.randn(n_trades)) * (volume / 500) ** 0.5
price_change = base_change + volume_effect * 2
price_change = np.clip(price_change, -8, 8)
volume = np.clip(volume, 0, 800)

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

# Custom colormap - trading green
colors = ['#0c1014', '#0a2018', '#0a3020', '#1a5030', '#2a7040', 
          '#3a9050', '#4ab060', '#6ad070', '#8af080', '#aaffa0', '#d0ffc0']
cmap = LinearSegmentedColormap.from_list('trading', colors, N=256)

# 2D histogram
h = ax.hist2d(price_change, volume, bins=[60, 50], cmap=cmap, cmin=1)

# Zero line
ax.axvline(x=0, color='#6ad070', linestyle='-', alpha=0.5, linewidth=2)

# High volume zone
ax.axhspan(400, 800, alpha=0.15, color='#ff6b6b', label='High Volume Zone')

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

ax.set_xlabel('Price Change (%)', fontsize=13, color='#a0e0b0', fontweight='600', labelpad=10)
ax.set_ylabel('Volume (thousands)', fontsize=13, color='#a0e0b0', fontweight='600', labelpad=10)
ax.set_title('Trading Volume vs Price Movement', fontsize=16, color='white', fontweight='bold', pad=20)

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

ax.legend(loc='upper right', fontsize=9, facecolor='#1a2820', edgecolor='#2a4030', labelcolor='#a0e0b0')
plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support