import lux
import pandas as pd
df = pd.read_csv("https://github.com/lux-org/lux-datasets/blob/master/data/hpi_cleaned.csv?raw=True")
df
df.intent = ["Inequality","AvrgLifeExpectancy"]
df
covid = pd.read_csv("https://github.com/lux-org/lux-datasets/blob/master/data/covid_cleaned.csv?raw=True")
df = covid.merge(df,left_on=["Entity","Code"],right_on=["Country","cca3"])
df
df.intent = ["Inequality","AvrgLifeExpectancy"]
df
df
df.exported
print(df.exported[0].to_code("altair"))
We can copy-and-paste the output Altair code into a separate cell. Then let's tweak the plotting code a bit before sharing this insight with our colleagues.
import altair as alt
c = "#e7298a"
chart = alt.Chart(df,title="Check out this cool insight!").mark_circle().encode(
x=alt.X('Inequality',scale=alt.Scale(domain=(0.04, 0.51)),type='quantitative', axis=alt.Axis(title='Inequality')),
y=alt.Y('AvrgLifeExpectancy',scale=alt.Scale(domain=(48.9, 83.6)),type='quantitative', axis=alt.Axis(title='AvrgLifeExpectancy'))
)
highlight = df[(df["Inequality"]>0.35)&(df["stringency_level"]=="High")]
hchart = alt.Chart(highlight).mark_point(color=c,size=50,shape="cross").encode(
x=alt.X('Inequality',scale=alt.Scale(domain=(0.04, 0.51)),type='quantitative', axis=alt.Axis(title='Inequality')),
y=alt.Y('AvrgLifeExpectancy',scale=alt.Scale(domain=(48.9, 83.6)),type='quantitative', axis=alt.Axis(title='AvrgLifeExpectancy')),
)
text = alt.Chart(highlight).mark_text(color=c,dx=-35,dy=0,fontWeight=800).encode(
x=alt.X('Inequality',scale=alt.Scale(domain=(0.04, 0.51)),type='quantitative', axis=alt.Axis(title='Inequality')),
y=alt.Y('AvrgLifeExpectancy',scale=alt.Scale(domain=(48.9, 83.6)),type='quantitative', axis=alt.Axis(title='AvrgLifeExpectancy')),
text=alt.Text('Country')
)
chart = chart.encode(color=alt.Color('stringency_level',type='nominal'))
chart = chart.properties(width=160,height=150)
(chart + hchart + text).configure_title(color=c)