Last active
September 16, 2020 16:32
-
-
Save wbecher/260f2e15d8d73b785ae76cc9e9eb3606 to your computer and use it in GitHub Desktop.
Testes: Streamlit com Altair e Vega Datasets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Antes de rodar: | |
| # pip install streamlit altair vega_datasets | |
| # streamlit run altair.py | |
| import streamlit as st | |
| import altair as alt | |
| from vega_datasets import data | |
| cars = data.cars() | |
| """ | |
| # Teste de Datasets Altair com Streamlit | |
| """ | |
| st.markdown('## HP x Milhas por galão x Cilindros') | |
| hp_ml_cyl = alt.Chart(cars).mark_point().properties(width=600, height=400).encode( | |
| x='Horsepower:Q', | |
| y='Miles_per_Gallon:Q', | |
| color='Cylinders:O' | |
| ) | |
| st.write(hp_ml_cyl) | |
| st.markdown('---') | |
| st.markdown('## HP x Milhas por galão x Origem') | |
| hp_ml_origin = alt.Chart(cars).mark_point().properties(width=600, height=400).encode( | |
| x='Horsepower:Q', | |
| y='Miles_per_Gallon:Q', | |
| color='Origin:N' | |
| ) | |
| st.write(hp_ml_origin) | |
| st.markdown('---') | |
| st.markdown('## HP x Milhas por galão x Origem com seletor') | |
| st.markdown('Clique e arraste nos gráficos, para destacar a seleção.') | |
| selection = alt.selection_interval() | |
| chart = alt.Chart(cars).mark_point().properties(width=270, height=270).encode( | |
| x='Horsepower:Q', | |
| y='Miles_per_Gallon:Q', | |
| color=alt.condition(selection, | |
| 'Origin:N', | |
| alt.value('gray')) | |
| ).add_selection( | |
| selection | |
| ) | |
| hist = alt.Chart(cars).mark_bar().encode( | |
| alt.X('count()', title='Quantidade de Carros'), | |
| alt.Y('Origin', title='País de Origem'), | |
| color='Origin' | |
| ).transform_filter( | |
| selection | |
| ).properties(width=600) | |
| st.write((chart | chart.encode(x='Acceleration:Q')) & hist) | |
| """ | |
| ## Referências | |
| - Grammar of Graphics: https://towardsdatascience.com/a-comprehensive-guide-to-the-grammar-of-graphics-for-effective-visualization-of-multi-dimensional-1f92b4ed4149?gi=b36667dc3c1d | |
| - Altair: https://altair-viz.github.io/index.html | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment