Altair是基于Vega-Lite的Python下的声明式统计可视化库。Altair是一个 Python统计可视化库。与Matplotlib 和Seaborn相比,Altair 更注重统计特征。Altair凭借其强大而简洁的可视化语法,可帮助你快速构建各种可视化效果。
-
Altair源码:
https://github.com/altair-viz/altair -
Altair文档:
https://altair-viz.github.io/ -
Vega-Lite源码:
https://github.com/vega/vega-lite -
Vega-Lite文档:
https://vega.github.io/vega-lite/
▍Altair库的安装
pip install altair
pip install altair vega_datasets
▍Altair库的实战
import pandas as pd
import altair as altfrom vega_datasets import data
cars = data.cars()
cars.head()chart = alt.Chart(cars)alt.Chart(cars).mark_bar().encode(
x=alt.X('Miles_per_Gallon', bin=alt.Bin(maxbins=30)),
y='count()',
color='Origin',
column='Origin'
)
alt.Chart(cars).mark_tick().encode(x='Miles_per_Gallon')
interval = alt.selection_interval()
base = alt.Chart(cars).mark_point().encode(y='Horsepower',color=alt.condition(interval, 'Origin', alt.value('lightgray')),tooltip='Name'
).properties(selection=interval
)
hist = alt.Chart(cars).mark_bar().encode(x='count()',y='Origin',color='Origin'
).properties(width=800,height=80
).transform_filter(interval
)
scatter = base.encode(x='Miles_per_Gallon') | base.encode(x='Acceleration')
scatter & hist
import altair as alt
from vega_datasets import datasource = data.stocks()alt.Chart(source).mark_line(point=alt.OverlayMarkDef(filled=False, fill="white")
).encode(x='date:T',y='price:Q',color='symbol:N'
)
import altair as alt
from vega_datasets import datasource = data.cars()alt.Chart(source).mark_circle().encode(alt.X(alt.repeat("column"), type='quantitative'),alt.Y(alt.repeat("row"), type='quantitative'),color='Origin:N'
).properties(width=150,height=150
).repeat(row=['Horsepower', 'Acceleration', 'Miles_per_Gallon'],column=['Miles_per_Gallon', 'Acceleration', 'Horsepower']
).interactive()
这些图是用Python的可视化库Altair绘制的,Altair可以使用强大而简洁的可视化语法快速开发各种统计可视化图表。用户只需要提供数据列与编码通道之间的链接,例如x轴,y轴,颜色等。