|
| 1 | +""" pyplots.ai |
| 2 | +volcano-basic: Volcano Plot for Statistical Significance |
| 3 | +Library: seaborn 0.13.2 | Python 3.13.11 |
| 4 | +Quality: 91/100 | Created: 2025-12-31 |
| 5 | +""" |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +import seaborn as sns |
| 11 | + |
| 12 | + |
| 13 | +# Data: Simulated differential expression results |
| 14 | +np.random.seed(42) |
| 15 | +n_genes = 500 |
| 16 | + |
| 17 | +# Generate log2 fold changes (centered around 0, some extreme values) |
| 18 | +log2_fold_change = np.concatenate( |
| 19 | + [ |
| 20 | + np.random.normal(0, 0.5, n_genes - 60), # Most genes: no change |
| 21 | + np.random.normal(-2.5, 0.5, 30), # Down-regulated |
| 22 | + np.random.normal(2.5, 0.5, 30), # Up-regulated |
| 23 | + ] |
| 24 | +) |
| 25 | + |
| 26 | +# Generate p-values (correlated with fold change magnitude for realism) |
| 27 | +base_pvalues = 10 ** (-np.abs(log2_fold_change) * np.random.uniform(0.5, 2, n_genes)) |
| 28 | +base_pvalues = np.clip(base_pvalues, 1e-10, 1.0) |
| 29 | +neg_log10_pvalue = -np.log10(base_pvalues) |
| 30 | + |
| 31 | +# Define significance thresholds |
| 32 | +pval_threshold = 1.3 # -log10(0.05) |
| 33 | +fc_threshold = 1.0 # log2(2) = 1 |
| 34 | + |
| 35 | +# Categorize genes using numpy vectorized conditions |
| 36 | +categories = np.where( |
| 37 | + neg_log10_pvalue < pval_threshold, |
| 38 | + "Not Significant", |
| 39 | + np.where( |
| 40 | + log2_fold_change > fc_threshold, |
| 41 | + "Up-regulated", |
| 42 | + np.where(log2_fold_change < -fc_threshold, "Down-regulated", "Not Significant"), |
| 43 | + ), |
| 44 | +) |
| 45 | + |
| 46 | +# Create DataFrame |
| 47 | +df = pd.DataFrame({"log2_fold_change": log2_fold_change, "neg_log10_pvalue": neg_log10_pvalue, "category": categories}) |
| 48 | + |
| 49 | +# Sort so significant points are plotted on top |
| 50 | +category_order = {"Not Significant": 0, "Down-regulated": 1, "Up-regulated": 2} |
| 51 | +df["order"] = df["category"].map(category_order) |
| 52 | +df = df.sort_values("order") |
| 53 | + |
| 54 | +# Color palette matching specification |
| 55 | +palette = { |
| 56 | + "Not Significant": "#888888", |
| 57 | + "Down-regulated": "#306998", # Python Blue |
| 58 | + "Up-regulated": "#E63946", # Red for up-regulated |
| 59 | +} |
| 60 | + |
| 61 | +# Create figure |
| 62 | +fig, ax = plt.subplots(figsize=(16, 9)) |
| 63 | + |
| 64 | +# Scatter plot using seaborn |
| 65 | +sns.scatterplot( |
| 66 | + data=df, |
| 67 | + x="log2_fold_change", |
| 68 | + y="neg_log10_pvalue", |
| 69 | + hue="category", |
| 70 | + hue_order=["Not Significant", "Down-regulated", "Up-regulated"], |
| 71 | + palette=palette, |
| 72 | + s=100, |
| 73 | + alpha=0.7, |
| 74 | + edgecolor="none", |
| 75 | + ax=ax, |
| 76 | + legend=True, |
| 77 | +) |
| 78 | + |
| 79 | +# Threshold lines |
| 80 | +ax.axhline(y=pval_threshold, color="#333333", linestyle="--", linewidth=2, alpha=0.7) |
| 81 | +ax.axvline(x=fc_threshold, color="#333333", linestyle="--", linewidth=2, alpha=0.7) |
| 82 | +ax.axvline(x=-fc_threshold, color="#333333", linestyle="--", linewidth=2, alpha=0.7) |
| 83 | + |
| 84 | +# Add threshold annotations |
| 85 | +ax.text(ax.get_xlim()[1] - 0.3, pval_threshold + 0.3, "p = 0.05", fontsize=14, ha="right", color="#333333") |
| 86 | +ax.text(fc_threshold + 0.1, ax.get_ylim()[1] - 0.5, "FC = 2", fontsize=14, ha="left", color="#333333") |
| 87 | +ax.text(-fc_threshold - 0.1, ax.get_ylim()[1] - 0.5, "FC = 0.5", fontsize=14, ha="right", color="#333333") |
| 88 | + |
| 89 | +# Labels and styling |
| 90 | +ax.set_xlabel("Log2 Fold Change", fontsize=20) |
| 91 | +ax.set_ylabel("-Log10(p-value)", fontsize=20) |
| 92 | +ax.set_title("volcano-basic · seaborn · pyplots.ai", fontsize=24) |
| 93 | +ax.tick_params(axis="both", labelsize=16) |
| 94 | +ax.grid(True, alpha=0.3, linestyle="--") |
| 95 | + |
| 96 | +# Legend styling |
| 97 | +ax.legend(title="Significance", fontsize=14, title_fontsize=16, loc="upper right", framealpha=0.9) |
| 98 | + |
| 99 | +plt.tight_layout() |
| 100 | +plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
0 commit comments