Altair 复合图表指南:分层、多视图与分面可视化
概述
在数据可视化领域,Altair 作为基于 Vega-Lite 的 Python 库,提供了强大的复合图表功能。本文将深入探讨 Altair 中的分层图表、多视图组合以及分面可视化技术,帮助您构建更复杂、信息更丰富的数据可视化作品。
复合图表类型总览
Altair 提供了多种复合图表类型,通过不同的组合方式可以实现丰富的可视化效果:
| 图表类 | 函数形式 | 运算符形式 | 主要用途 | |-------------------|--------------------------|------------------|------------------| | LayerChart | alt.layer(chart1, chart2)
| chart1 + chart2
| 创建分层叠加图表 | | HConcatChart | alt.hconcat(chart1, chart2)
| chart1 | chart2
| 水平并排图表 | | VConcatChart | alt.vconcat(chart1, chart2)
| chart1 & chart2
| 垂直堆叠图表 | | RepeatChart | chart.repeat(row, column)
| - | 重复图表模式 | | FacetChart | chart.facet(facet, row, column)
| - | 数据分面展示 |
分层图表 (LayerChart)
分层图表允许在同一个坐标系上叠加多个图表层,非常适合需要同时展示多种标记类型的场景。
基本用法
import altair as alt
from vega_datasets import data
stocks = data.stocks.url
base = alt.Chart(stocks).encode(
x='date:T',
y='price:Q',
color='symbol:N'
).transform_filter(
alt.datum.symbol == 'GOOG'
)
# 运算符形式
line_with_points = base.mark_line() + base.mark_point()
# 函数形式
multi_layer = alt.layer(
base.mark_line(),
base.mark_point(),
base.mark_rule()
).interactive()
图层顺序的重要性
分层图表中,图层的绘制顺序遵循"先添加的在下层"原则。例如在热力图上方添加点标记:
heatmap = alt.Chart(source).mark_rect().encode(
alt.X('IMDB_Rating:Q').bin(),
alt.Y('Rotten_Tomatoes_Rating:Q').bin(),
alt.Color('count()').scale(scheme='greenblue')
)
points = alt.Chart(source).mark_circle(
color='black',
size=5,
).encode(
x='IMDB_Rating:Q',
y='Rotten_Tomatoes_Rating:Q',
)
# 正确的叠加顺序
effective_visual = heatmap + points
如果顺序颠倒(points + heatmap
),点标记将被热力图覆盖而难以辨认。
水平与垂直组合图表
水平组合 (HConcatChart)
水平组合适合并排展示相关图表,便于比较:
# 散点图
chart1 = alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N'
).properties(
height=300,
width=300
)
# 直方图
chart2 = alt.Chart(iris).mark_bar().encode(
x='count()',
y=alt.Y('petalWidth:Q').bin(maxbins=30),
color='species:N'
).properties(
height=300,
width=100
)
# 两种等效写法
side_by_side = chart1 | chart2
# 或
side_by_side = alt.hconcat(chart1, chart2)
垂直组合 (VConcatChart)
垂直组合常用于创建交互式的刷选关联视图:
brush = alt.selection_interval(encodings=['x'])
base = alt.Chart(source).mark_area().encode(
x = 'date:T',
y = 'price:Q'
).properties(
width=600,
height=200
)
upper = base.encode(alt.X('date:T').scale(domain=brush))
lower = base.properties(height=60).add_params(brush)
linked_view = upper & lower # 等价于 alt.vconcat(upper, lower)
重复图表模式 (RepeatChart)
重复图表提供了一种高效创建多面板视图的方法,特别适合探索多维数据:
alt.Chart(iris).mark_point().encode(
alt.X(alt.repeat("column"), type='quantitative'),
alt.Y(alt.repeat("row"), type='quantitative'),
color='species:N'
).properties(
width=200,
height=200
).repeat(
row=['petalLength', 'petalWidth'],
column=['sepalLength', 'sepalWidth']
).interactive()
分层重复模式
重复模式也可用于创建分层图表:
alt.Chart(source).mark_line().encode(
x=alt.X("IMDB_Rating").bin(),
y=alt.Y(alt.repeat('layer')).aggregate('mean'),
color=alt.ColorDatum(alt.repeat('layer'))
).repeat(layer=["US_Gross", "Worldwide_Gross"])
分面图表 (FacetChart)
分面图表(又称小多图)能够按照数据类别自动分组展示:
基本分面
# 使用facet方法
alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N'
).properties(
width=180,
height=180
).facet(
column='species:N'
)
# 使用column编码(简单情况)
alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N',
column='species:N'
)
复杂分面示例
分面可以应用于复合图表,创建丰富的交互式可视化:
hover = alt.selection_point(on='pointerover', nearest=True, empty=False)
when_hover = alt.when(hover)
base = alt.Chart(iris).encode(
x='petalLength:Q',
y='petalWidth:Q',
color=when_hover.then("species:N").otherwise(alt.value("lightgray"))
).properties(
width=180,
height=180,
)
points = base.mark_point().add_params(hover)
text = base.mark_text(dy=-5).encode(
text="species:N",
opacity=when_hover.then(1).otherwise(0),
)
(points + text).facet("species:N")
最佳实践与选择指南
- 简单叠加:使用分层图表(LayerChart)
- 并排比较:使用水平/垂直组合(HConcat/VConcat)
- 多维探索:优先考虑重复图表(RepeatChart)
- 类别展示:分面图表(FacetChart)最为合适
记住,这些复合图表技术可以自由组合使用,创造出无限可能的可视化效果。通过实践这些模式,您将能够更高效地传达数据中的复杂关系和模式。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考