KDE Plot
House Price Distribution
KDE of residential property prices by neighborhood.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
np.random.seed(106)
downtown = np.random.lognormal(13.2, 0.4, 300)
suburban = np.random.lognormal(12.8, 0.3, 500)
rural = np.random.lognormal(12.2, 0.35, 200)
fig, ax = plt.subplots(figsize=(12, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
x = np.linspace(100000, 1500000, 500)
areas = [
(downtown, 'Downtown', '#4927F5'),
(suburban, 'Suburban', '#27D3F5'),
(rural, 'Rural', '#6CF527'),
]
for data, label, color in areas:
kde = stats.gaussian_kde(data)
y = kde(x)
median = np.median(data)
ax.fill_between(x, y, alpha=0.3, color=color)
ax.plot(x, y, color=color, linewidth=2.5, label=label + ' ($' + str(int(median/1000)) + 'K)')
ax.set_xlabel('Price ($)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('House Price Distribution by Area', fontsize=16, color='#1f2937', fontweight='bold', pad=15)
ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: '$' + str(int(x/1000)) + 'K'))
ax.tick_params(colors='#374151', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#d1d5db')
ax.legend(loc='upper right', facecolor='#f9fafb', edgecolor='#d1d5db', labelcolor='#374151')
ax.grid(True, alpha=0.3, color='#e5e7eb')
ax.set_xlim(100000, 1500000)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕