Matplotlib Volcano Plot

How to create a volcano plot using the Matplotlib library.

Python
import numpy as np
import matplotlib.pyplot as plt

# Generate random data
np.random.seed(0)
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)

# Calculate log fold change and p-values
log_fold_change = np.log2(x / y)
p_values = -np.log10(np.random.uniform(0, 1, 100))

# Create volcano plot
plt.scatter(log_fold_change, p_values, color='black', alpha=0.5)
plt.axhline(y=-np.log10(0.05), color='red', linestyle='--')
plt.axvline(x=0, color='gray', linestyle='--')
plt.xlabel('Log Fold Change')
plt.ylabel('-log10(p-value)')
plt.title('Volcano Plot')
plt.show()
Click Run or press shift + ENTER to run code.