1

I have a time series dataset and I wanted to plot line charts that display monthly and yearly changes.Some code provided below as an example. But instead of making two static charts, I'd like to have one interactive chart that allows me to change toggle between yearly and monthly chart by clicking radio buttons. I am not sure if this is possible.

import altair as alt
from vega_datasets import data

source = data.stocks()

base = alt.Chart(source).encode(
x=alt.X('date:T',axis=alt.Axis(format="%Y-%b",grid=False)),
y=alt.Y('price:Q'),
color='symbol:N'
 )

lines = base.mark_line().encode()
lines

enter image description here

source = data.stocks()
source['year']=source.date.dt.year

base = alt.Chart(source).encode(
x='year:N',
y='avg_price:Q',
color='symbol:N'
).transform_aggregate(
                avg_price='mean(price)',
                groupby=['symbol','year'])

points = base.mark_line().encode(
opacity=alt.value(1)
).properties(
width=600
)
points 

enter image description here

shimiao
  • 81
  • 4

0 Answers0