import altair as alt
import pandas as pd
def plot_curve(half_life, data):
y = data['% of atoms left']
x = pd.date_range(start='1970-01-01', periods=len(y), freq='MS')
return alt.Chart(x, y).mark_line().encode(
x='date:T',
y=':Y',
color=':C',
title='Half Life Curve',
colorAxis=alt.ColorAxis(encode=alt.X(alt.condition(alt.datatype('date')),
color='Half Life')),
yaxis=alt.YAxis(title='Percentage of Atoms Left')
)
# Sample Data
data = pd.DataFrame({'Half Life': [20, 15, 10, 5, 2]})
# Generate X-axis Data
date_range = pd.date_range(start='1970-01-01', periods=len(data), freq='MS')
date_range = alt.Chart(date_range).mark_rect().encode(
x='datetime:O',
y='altSpace:Y',
color='altY:C',
stylers={
'fill': {
'color': '#0080c0'
}
}
)
# Create the Final Chart
chart = plot_curve(data['Half Life'], data)
chart = chart.merge(date_range, left='x', right='x')
chart.show()
This program uses the Altair library to generate a line chart of the percentage of atoms left over time. The plot_curve function takes a half life and a data dataframe as inputs and returns a chart object. The date_range function generates an X-axis timeline using the Altair chart function, and the final chart is created by merging the curve chart with the timeline. The resulting chart can be displayed