Altair Volcano Plot

How to create a volcano plot using the Altair library.

Python
import numpy as np
import pandas as pd
import altair as alt

# 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 DataFrame
data = pd.DataFrame({'Log Fold Change': log_fold_change, '-log10(p-value)': p_values})

# Create Altair chart
chart = alt.Chart(data).mark_circle().encode(
    x='Log Fold Change',
    y='-log10(p-value)',
    tooltip=['Log Fold Change', '-log10(p-value)']
).properties(
    title='Volcano Plot',
    width=500,
    height=400
)

# Show the chart with hover interaction
chart.interactive().show()
Click Run or press shift + ENTER to run code.