Violin Plot

Property Price Distribution

Neighborhood housing prices with market trends

Output
Property Price Distribution
Python
import matplotlib.pyplot as plt
import numpy as np

# Property prices by neighborhood (in $1000s)
np.random.seed(42)
neighborhoods = ['Downtown', 'Midtown', 'Suburbs', 'Waterfront', 'Historic\nDistrict']
prices = [
    np.random.lognormal(6.2, 0.4, 150),   # Downtown - high, varied
    np.random.lognormal(5.9, 0.35, 150),  # Midtown
    np.random.lognormal(5.5, 0.3, 150),   # Suburbs - lower
    np.random.lognormal(6.4, 0.5, 150),   # Waterfront - premium
    np.random.lognormal(5.7, 0.45, 150),  # Historic
]
# Convert to thousands
prices = [p / 1000 for p in prices]

# Luxury gradient
colors = ['#6366F1', '#8B5CF6', '#22C55E', '#0EA5E9', '#F59E0B']

# Create figure
fig, ax = plt.subplots(figsize=(12, 7), facecolor='white')

vp = ax.violinplot(prices, positions=range(len(neighborhoods)), widths=0.7,
                   showmeans=False, showmedians=False, showextrema=False)

for i, body in enumerate(vp['bodies']):
    body.set_facecolor(colors[i])
    body.set_edgecolor('white')
    body.set_linewidth(2)
    body.set_alpha(0.7)
    
    # Elegant shadow
    path = body.get_paths()[0]
    shadow = path.vertices.copy()
    shadow[:, 0] += 0.03
    shadow[:, 1] -= 15
    ax.fill(shadow[:, 0], shadow[:, 1], color='black', alpha=0.03)

# Market median reference
market_median = np.median([np.median(p) for p in prices])
ax.axhline(market_median, color='#6B7280', linewidth=1.5, linestyle='--', alpha=0.6)
ax.text(4.55, market_median + 20, 'Market\nMedian', fontsize=9, 
        color='#6B7280', va='bottom')

# Price statistics
for i, price in enumerate(prices):
    median = np.median(price)
    q1, q3 = np.percentile(price, [25, 75])
    
    # IQR band
    ax.fill_between([i - 0.06, i + 0.06], q1, q3,
                    color=colors[i], alpha=0.5, zorder=5)
    
    # Median marker
    ax.scatter(i, median, c='white', s=120, zorder=10,
               edgecolor=colors[i], linewidth=2.5, marker='D')
    
    # Price label
    ax.text(i, median + 50, f'${median:.0f}K', ha='center', fontsize=10,
            color='#374151', fontweight='600')
    
    # YoY change (simulated)
    yoy = np.random.uniform(-5, 15)
    color_yoy = '#10B981' if yoy > 0 else '#EF4444'
    ax.text(i, 50, f'{yoy:+.1f}%', ha='center', fontsize=9,
            color=color_yoy, fontweight='500')

# Label
ax.text(-0.5, 70, 'YoY Change', fontsize=9, color='#6B7280')

# Styling
ax.set_xticks(range(len(neighborhoods)))
ax.set_xticklabels(neighborhoods, fontsize=11, fontweight='600')
ax.set_ylabel('Price ($K)', fontsize=12, fontweight='500', color='#374151')
ax.set_ylim(0, 1200)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#E5E7EB')
ax.spines['bottom'].set_color('#E5E7EB')
ax.tick_params(colors='#6B7280', labelsize=10)
ax.yaxis.grid(True, linestyle='--', alpha=0.3, color='#9CA3AF')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support