Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy natural_disasters.py example #2643

Merged
merged 9 commits into from
Jul 4, 2022
51 changes: 37 additions & 14 deletions altair/examples/natural_disasters.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
"""
Natural Disasters
-----------------
This example shows a visualization of global deaths from natural disasters.
Global Deaths from Natural Disasters
------------------------------------
This example shows a proportional symbols visualization of deaths from natural disasters by year and type.
"""
# category: case studies
import altair as alt
from vega_datasets import data

source = data.disasters.url

alt.Chart(source).mark_circle(
alt.Chart(source).transform_filter(
alt.datum.Entity != 'All natural disasters'
).mark_circle(
opacity=0.8,
stroke='black',
strokeWidth=1
strokeWidth=1,
strokeOpacity=0.4
).encode(
alt.X('Year:O', axis=alt.Axis(labelAngle=0)),
alt.Y('Entity:N'),
alt.Size('Deaths:Q',
scale=alt.Scale(range=[0, 4000]),
legend=alt.Legend(title='Annual Global Deaths')
x=alt.X('Year:T', title=None, scale=alt.Scale(domain=['1899','2018'])),
y=alt.Y(
'Entity:N',
sort=alt.EncodingSortField(field="Deaths", op="sum", order='descending'),
title=None
),
size=alt.Size('Deaths:Q',
scale=alt.Scale(range=[0, 2500]),
legend=alt.Legend(title='Deaths', clipHeight=30, format='s')
),
alt.Color('Entity:N', legend=None)
color=alt.Color('Entity:N', legend=None),
tooltip=[
"Entity:N",
alt.Tooltip("Year:T", format='%Y'),
alt.Tooltip("Deaths:Q", format='~s')
],
).properties(
width=450,
height=320
).transform_filter(
alt.datum.Entity != 'All natural disasters'
height=320,
title=alt.TitleParams(
text="Global Deaths from Natural Disasters (1900-2017)",
subtitle="The size of the bubble represents the total death count per year, by type of disaster",
anchor='start'
)
).configure_axisY(
domain=False,
ticks=False,
offset=10
).configure_axisX(
grid=False,
).configure_view(
stroke=None
)