KDE Plot
Gene Expression Level KDE
Cumulative density of gene expression levels in cancer vs normal tissue
Output
Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
np.random.seed(789)
BG_COLOR = '#0d1117'
TEXT_COLOR = 'white'
# Expression levels (log2 TPM)
normal = np.random.normal(5, 1.5, 600)
tumor = np.random.normal(7, 2.0, 600)
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
x_range = np.linspace(0, 14, 500)
for data, color, label in [(normal, '#27F5B0', 'Normal Tissue'),
(tumor, '#F5276C', 'Tumor Tissue')]:
kde = gaussian_kde(data)
cdf = np.array([kde.integrate_box_1d(-np.inf, x) for x in x_range])
ax.plot(x_range, cdf, color=color, linewidth=3, label=label)
ax.axhline(0.5, color='#888888', linestyle=':', alpha=0.7)
ax.text(12, 0.52, 'Median', color='#888888', fontsize=10)
ax.set_xlabel('Expression Level (log2 TPM)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Cumulative Probability', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Gene Expression: Cumulative Distribution', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)
ax.tick_params(colors='#888888', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#333333')
ax.legend(facecolor=BG_COLOR, edgecolor='#333333', labelcolor=TEXT_COLOR, fontsize=10, loc='lower right')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕