Skip to content

Instantly share code, notes, and snippets.

@whiledoing
Last active January 21, 2020 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whiledoing/e5dc5823cd51c3c9d2c5902b7b3cbd1b to your computer and use it in GitHub Desktop.
Save whiledoing/e5dc5823cd51c3c9d2c5902b7b3cbd1b to your computer and use it in GitHub Desktop.
[altair-snippets] altair snippets #python #altair #visualization
# load an example dataset
from vega_datasets import data
cars = data.cars()
import altair as alt
interval = alt.selection_interval()
base = alt.Chart(cars).mark_point().encode(
y='Miles_per_Gallon',
color=alt.condition(interval, 'Origin', alt.value('lightgray'))
).properties(
selection=interval
)
# 这个非常好,使用encode来填充确实的参数,便于做灵活的FacetGrid的图形
base.encode(x='Acceleration') | base.encode(x='Horsepower')
# load an example dataset
from vega_datasets import data
cars = data.cars()
import altair as alt
# 年为X轴,数值为Y,可以看到一年内多个不同的数值为一个竖线。使用Origin做颜色的分类。
points = alt.Chart(cars).mark_point().encode(
x='Year:T',
y='Miles_per_Gallon',
color='Origin'
).properties(
width=800
)
# 平均值折线计算
lines = alt.Chart(cars).mark_line().encode(
x='Year:T',
y='mean(Miles_per_Gallon)',
color='Origin'
).properties(
width=800
).interactive(bind_y=False)
# 使用+放在一个图中
points + lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment