我无法在Altair的县级choropleth上覆盖州轮廓。我正在使用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)
(此外,我指定了fillOpacity=0
,因为该默认值将在Vega-Lite 4中更改。