""" Layered Bar Chart ----------------- This example shows a bar chart showing the US population distribution of age groups and gender in 2000. """ # category: bar charts import altair as alt from altair.expr import datum, if_ from vega_datasets import data source = data.population.url alt.Chart(source).mark_bar(opacity=0.7).encode( alt.X('age:O', scale=alt.Scale(rangeStep=17)), alt.Y('sum(people):Q', axis=alt.Axis(title='population'), stack=None), alt.Color('gender:N', scale=alt.Scale(range=["#EA98D2", "#659CCA"]))).transform_filter( datum.year == 2000).transform_calculate( "gender", if_(datum.sex == 2, 'Female', 'Male'))
chart = alt.Chart( currency_hist_20WBTC.reset_index(), title="{} Price vs Time".format(df_choice)).mark_circle( size=70, opacity=0.6).encode( alt.X('index', axis=alt.Axis(title='Date')), alt.Y('price_x', axis=alt.Axis(title='Price'), scale=alt.Scale(type='log')), color=alt.Color( 'price above BTC 20W MA', scale=alt.Scale(scheme='category20'), legend=alt.Legend( title="BTC Price"))).transform_calculate( 'price above BTC 20W MA', if_(datum['price above BTC 20W MA'] == 0, 'BTC Below 20 Week MA', 'BTC Above 20 Week MA')).interactive() #st.line_chart(currency_hist['price']) st.altair_chart(chart, use_container_width=True) else: line_chart = alt.Chart( currency_hist_20WBTC.reset_index(), title="{} Price vs Time".format(df_choice)).mark_line().encode( alt.X('index', axis=alt.Axis(title='Date')), alt.Y('price_x', axis=alt.Axis(title='Price'), scale=alt.Scale(type='log'))).interactive() st.altair_chart(line_chart, use_container_width=True)
""" US Population Over Time ======================= This chart visualizes the age distribution of the US population over time. It uses a slider widget that is bound to the year to visualize the age distribution over time. """ # category: interactive import altair as alt from altair.expr import datum, if_ from vega_datasets import data pop = data.population.url pink_blue = alt.Scale(domain=('Male', 'Female'), range=["steelblue", "salmon"]) slider = alt.binding_range(min=1900, max=2000, step=10) year = alt.selection_single(name="year", fields=['year'], bind=slider) chart = alt.Chart(pop).mark_bar().encode( x=alt.X('sex:N', axis=alt.Axis(title=None)), y=alt.Y('people:Q', scale=alt.Scale(domain=(0, 12000000))), color=alt.Color('sex:N', scale=pink_blue), column='age:O').properties( width=20, selection=year, ).transform_calculate("sex", if_(datum.sex == 1, "Male", "Female")).transform_filter(year.ref())
It uses a slider widget that is bound to the year to visualize the age distribution over time. ''' # category: case studies import altair as alt from altair.expr import datum, if_ from vega_datasets import data pop = data.population.url slider = alt.binding_range(min=1850, max=2000, step=10) select_year = alt.selection_single(name='year', fields=['year'], bind=slider) base = alt.Chart(pop).add_selection(select_year).transform_filter( select_year).transform_calculate( gender=if_(datum.sex == 1, 'Male', 'Female')) title = alt.Axis(title='population') color_scale = alt.Scale(domain=['Male', 'Female'], range=['#1f77b4', '#e377c2']) left = base.transform_filter(datum.gender == 'Female').encode( y=alt.X('age:O', axis=None), x=alt.X('sum(people):Q', axis=title, sort=alt.SortOrder('descending')), color=alt.Color('gender:N', scale=color_scale, legend=None)).mark_bar().properties(title='Female') middle = base.encode( y=alt.X('age:O', axis=None), text=alt.Text('age:Q'), ).mark_text().properties(width=20)
""" US Population Over Time ======================= This chart visualizes the age distribution of the US population over time. It uses a slider widget that is bound to the year to visualize the age distribution over time. """ # category: interactive charts import altair as alt from altair.expr import datum, if_ from vega_datasets import data pop = data.population.url pink_blue = alt.Scale(domain=('Male', 'Female'), range=["steelblue", "salmon"]) slider = alt.binding_range(min=1900, max=2000, step=10) select_year = alt.selection_single(name="year", fields=['year'], bind=slider) alt.Chart(pop).mark_bar().encode( x=alt.X('sex:N', axis=alt.Axis(title=None)), y=alt.Y('people:Q', scale=alt.Scale(domain=(0, 12000000))), color=alt.Color('sex:N', scale=pink_blue), column='age:O').properties( width=20).add_selection(select_year).transform_calculate( "sex", if_(datum.sex == 1, "Male", "Female")).transform_filter(select_year)
""" Grouped Bar Chart ----------------------- This example shows a population broken out by gender and age for a specific year. The grouping is achieved by building a trellis plot with narrow column encoded on the age groups and x-axes encoded on gender. """ import altair as alt from altair.expr import datum, if_ from vega_datasets import data source = data.population.url chart = alt.Chart(source).mark_bar(stroke = 'transparent').encode( alt.X('gender:N', scale=alt.Scale(rangeStep = 12), axis=alt.Axis(title='')), alt.Y('sum(people):Q', axis=alt.Axis(title='population', grid=False)), color = alt.Color('gender:N', scale=alt.Scale(range=["#EA98D2", "#659CCA"])), column = 'age:O' ).configure_view( stroke='transparent' ).configure_axis( domainWidth=0.8 ).transform_filter( datum.year == 2000 ).transform_calculate( 'gender', if_(datum.sex == 2, 'Female', 'Male') )