Skip to content

Instantly share code, notes, and snippets.

@certik
Created April 8, 2026 17:53
Show Gist options
  • Select an option

  • Save certik/c51a4a4bf630051168d56b95dc31b1f9 to your computer and use it in GitHub Desktop.

Select an option

Save certik/c51a4a4bf630051168d56b95dc31b1f9 to your computer and use it in GitHub Desktop.
import altair as alt
import numpy as np
import pandas as pd
# Create the same data as p2-mpl.py
x1 = np.linspace(0.0, 5.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
x2 = np.linspace(0.0, 2.0)
y2 = np.cos(2 * np.pi * x2)
df1 = pd.DataFrame({'x': x1, 'y': y1})
df2 = pd.DataFrame({'x': x2, 'y': y2})
# Top subplot: damped oscillation with circle markers and line
line1 = alt.Chart(df1).mark_line().encode(
x=alt.X('x:Q'),
y=alt.Y('y:Q', title='Damped oscillation'),
)
points1 = alt.Chart(df1).mark_circle(size=40).encode(
x=alt.X('x:Q'),
y=alt.Y('y:Q', title='Damped oscillation'),
)
chart1 = (line1 + points1).properties(width=500, height=200)
# Bottom subplot: undamped oscillation with small dot markers and line
line2 = alt.Chart(df2).mark_line().encode(
x=alt.X('x:Q', title='time (s)'),
y=alt.Y('y:Q', title='Undamped'),
)
points2 = alt.Chart(df2).mark_point(size=15, filled=True).encode(
x=alt.X('x:Q', title='time (s)'),
y=alt.Y('y:Q', title='Undamped'),
)
chart2 = (line2 + points2).properties(width=500, height=200)
# Combine vertically with shared title
chart = alt.vconcat(
chart1, chart2,
title=alt.TitleParams('A tale of 2 subplots', anchor='middle'),
).resolve_scale(x='independent', y='independent')
chart.save('p2.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment