Creating an interactive chart that combines maximum daily temperature, percipitation, date, type of weather and allows interactive filtering.
python
altair
visualisation
Published
November 15, 2022
Code
import altair as altfrom vega_datasets import dataimport pandas as pdsource = data.seattle_weather()scale = alt.Scale(domain=['sun', 'fog', 'drizzle', 'rain', 'snow'],range=['#e7ba52', '#a7a7a7', '#aec7e8', '#1f77b4', '#9467bd'])color = alt.Color('weather:N', scale=scale)# We create two selections:# - a brush that is active on the top panel# - a multi-click that is active on the bottom panelbrush = alt.selection_interval(encodings=['x'])click = alt.selection_multi(encodings=['color'])# Top panel is scatter plot of temperature vs timepoints = alt.Chart().mark_point().encode( alt.X('monthdate(date):T', title='Date', scale=alt.Scale(domain=[pd.Timestamp(2012, 1, 1), pd.Timestamp(2012, 12, 31)])), alt.Y('temp_max:Q', title='Maximum Daily Temperature (C)', scale=alt.Scale(domain=[-5, 40]) ), color=alt.condition(brush, color, alt.value('lightgray')), size=alt.Size('precipitation:Q', scale=alt.Scale(range=[5, 200]))).properties( width=550, height=300).add_selection( brush).transform_filter( click)# Bottom panel is a bar chart of weather typebars = alt.Chart().mark_bar().encode( x='count()', y='weather:N', color=alt.condition(click, color, alt.value('lightgray')),).transform_filter( brush).properties( width=550,).add_selection( click)alt.vconcat( points, bars, data=source, title="Seattle Weather: 2012-2015")