import altair as alt
from vega_datasets import data
population = data.population()
y2k_pop = population.query('year == 2000')
alt.Chart(y2k_pop).mark_bar().encode(
x='sum(people)',
)
alt.Chart(y2k_pop).mark_bar().encode(
x='sum(people)',
y='age:O'
)
import pandas as pd
import numpy as np
data = pd.DataFrame()
data['counts'] = pd.Series(np.round(100 * np.abs(np.random.randn(8))))
data.index = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
data['title'] = data.index
data
counts | title | |
---|---|---|
a | 9.0 | a |
b | 186.0 | b |
c | 49.0 | c |
d | 42.0 | d |
e | 16.0 | e |
f | 48.0 | f |
g | 58.0 | g |
h | 28.0 | h |
alt.Chart(data).mark_bar().encode(
x=alt.X('counts', axis=alt.Axis(title='Counts')),
y=alt.Y('title',
sort=alt.EncodingSortField(field='counts', order='ascending', op='sum'),
axis=alt.Axis(title='Title')
),
color=alt.Color('title:N')
)
chart = alt.Chart(y2k_pop).mark_bar().encode(
x='age:O',
y='sum(people):Q',
color=alt.Color('sex:N', scale=alt.Scale(range=["#e377c2","#1f77b4"]))
)
chart
chart = alt.Chart(population).transform_filter(
"datum.year == 2000"
).transform_calculate(
"sex", "datum.sex == 1 ? 'Male' : 'Female'"
).encode(
color=alt.Color('sex:N', scale=alt.Scale(range=["#e377c2","#1f77b4"]))
)
chart.mark_bar().encode(
x='age:O',
y='sum(people):Q',
)
Re-using the transform from above:
chart.mark_bar().encode(
x='age:O',
y=alt.Y('sum(people):Q', stack=None),
opacity=alt.value(0.7)
)
chart.mark_bar().encode(
x='age:O',
y=alt.Y('sum(people):Q', stack='normalize'),
opacity=alt.value(0.7)
)
chart.mark_bar().encode(
alt.X('sex:N', axis=alt.Axis(title=None, labels=False)),
alt.Y('sum(people):Q'),
alt.Column('age:O')
).properties(
width=20 # width of one column facet
)
from vega_datasets import data
movies = data.movies()
alt.Chart(movies).mark_bar().encode(
x=alt.X('IMDB_Rating', bin=alt.Bin(maxbins=10)),
y='count(*):Q',
)