python-3.x - 在 Altair 的等值区域上叠加状态轮廓

标签 python-3.x vega altair vega-lite

我无法将州轮廓覆盖在 Altair 的县级分区统计图上。
我正在使用 Altair 的图层方法来实现这一点。
但是,它采用州轮廓图的笔画颜色并用该颜色绘制县轮廓。

这是我的代码:

import altair as alt
from vega_datasets import data

us_states = alt.topo_feature(data.us_10m.url, 'states')
us_counties = alt.topo_feature(data.us_10m.url, 'counties')
unemp_data = data.unemployment(sep='\t')
unemp_data.head()

plot = alt.Chart(us_counties).mark_geoshape(stroke='white').project(
type='albersUsa'
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(unemp_data, 'id', ['rate'])
).encode(
    color='rate:Q'
).properties(
    width=700,
    height=400
)

outline = alt.Chart(us_states).mark_geoshape( stroke='black').project(
    type='albersUsa'
).properties(
    width=700,
    height=400
)

alt.layer(plot,outline)

我得到以下结果: Layered Plot

最佳答案

这看起来是 Vega 中的一个错误,其中如果两个数据源相同,则笔划属性会相互覆盖。我设法通过在其中一个 URL 的末尾添加 "#" 来欺骗 Vega 认为数据集不同来解决此问题:

import altair as alt
from vega_datasets import data

us_states = alt.topo_feature(data.us_10m.url, 'states')
us_counties = alt.topo_feature(data.us_10m.url+"#", 'counties')
unemp_data = data.unemployment.url

plot = alt.Chart(us_counties).mark_geoshape(stroke='white').project(
type='albersUsa'
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(unemp_data, 'id', ['rate'])
).encode(
    color='rate:Q'
).properties(
    width=700,
    height=400
)

outline = alt.Chart(us_states).mark_geoshape(stroke='black', fillOpacity=0).project(
    type='albersUsa'
).properties(
    width=700,
    height=400
)

alt.layer(plot,outline)

enter image description here

(此外,我指定了 fillOpacity=0 因为此默认值将在 Vega-Lite 4 中更改)。

关于python-3.x - 在 Altair 的等值区域上叠加状态轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59222697/

相关文章:

d3.js - 如何更改 Vega-lite 条形图中条形的颜色?

Vega-Lite - 处理大量数据的最佳方式是什么?

python - 是否可以在 Altair 中使用多个数据源对分层图表进行分面?

altair - 如何显式设置 mark_rect 的 y 范围(热图)

json - Deneb 可视面积图 Power BI

python - 使用 Panel 时不显示 Altair 图

python - 为什么 python 在定义时而不是在调用时评估函数的参数?

python-3.x - 无法 pip 安装 egenix-mx-base

python-3.x - 脚本中的 SSH -f - 执行命令时获取信号

Python:如何完整打印出字典的前n个键和值