2D Histogram

Song Tempo vs Energy

2D histogram of music tracks tempo versus energy level analysis.

Output
Song Tempo vs Energy
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(42)

# Music features
tempo = np.concatenate([
    np.random.normal(80, 15, 1500),   # Slow songs
    np.random.normal(120, 20, 3000),  # Pop/rock
    np.random.normal(160, 15, 1500)   # Electronic/dance
])
energy = 0.2 + tempo/200 * 0.5 + np.random.beta(2, 2, len(tempo)) * 0.3
energy = np.clip(energy, 0, 1)

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

# Custom colormap: pink to yellow_lime
colors = ['#020B14', '#2d0d2d', '#F527B0', '#D3F527']
cmap = LinearSegmentedColormap.from_list('pink_lime', colors, N=256)

h = ax.hist2d(tempo, energy, bins=45, cmap=cmap, cmin=1)
cbar = plt.colorbar(h[3], ax=ax, pad=0.02)
cbar.set_label('Tracks', color='white', fontsize=11)
cbar.ax.yaxis.set_tick_params(color='white')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='white')

ax.set_xlabel('Tempo (BPM)', fontsize=11, color='white', fontweight='500')
ax.set_ylabel('Energy', fontsize=11, color='white', fontweight='500')
ax.set_title('Song Tempo vs Energy', fontsize=14, color='white', fontweight='bold', pad=15)

ax.tick_params(colors='white', labelsize=9)
for spine in ax.spines.values():
    spine.set_color('#333333')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support