1. Grundlagen¶
aus https://altair-viz.github.io/gallery/simple_bar_chart.html
import altair as alt
import pandas as pd
source = pd.DataFrame({
'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})
alt.Chart(source).mark_bar().encode(
x='a',
y='b'
)
import altair as alt
import numpy as np
import pandas as pd
x = np.arange(100)
source = pd.DataFrame({
'x': x,
'f(x)': np.sin(x / 5)
})
alt.Chart(source).mark_line().encode(
x='x',
y='f(x)'
)
import altair as alt
from vega_datasets import data
source = data.cars()
alt.Chart(source).mark_circle(size=60).encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']
).interactive()
import altair as alt
from vega_datasets import data
source = data.iowa_electricity()
alt.Chart(source).mark_area().encode(
x="year:T",
y="net_generation:Q",
color="source:N"
)
aus https://altair-viz.github.io/gallery/natural_disasters.html
import altair as alt
from vega_datasets import data
source = data.disasters.url
alt.Chart(source).mark_circle(
opacity=0.8,
stroke='black',
strokeWidth=1
).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')
),
alt.Color('Entity:N', legend=None)
).properties(
width=450,
height=320
).transform_filter(
alt.datum.Entity != 'All natural disasters'
)
aus https://altair-viz.github.io/gallery/seattle_weather_interactive.html
import altair as alt
from vega_datasets import data
source = 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 panel
brush = alt.selection_interval(encodings=['x'])
click = alt.selection_multi(encodings=['color'])
# Top panel is scatter plot of temperature vs time
points = alt.Chart().mark_point().encode(
alt.X('monthdate(date):T', title='Date'),
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 type
bars = 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"
)