牛郎星全息图中缺失值/空值的处理 [英] Dealing with missing values / nulls in Altair choropleth map

查看:41
本文介绍了牛郎星全息图中缺失值/空值的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用美国州级数据在牛郎星创建了一张Cholopeth地图。然而,我没有一些州的数据。默认情况下,这些州根本不会出现在地图上。示例图片如下:

我希望空州在地图上显示为浅灰色。牛郎星文档显示了另一张符合此描述的地图:

我的问题是如何使第一张地图中带有空值的州看起来像第二张地图中的州。我试过几种方法。以下是我的原始地图代码:

states = alt.topo_feature(data.us_10m.url, 'states')
source = df

alt.Chart(states).mark_geoshape().encode(
    color=alt.Color('avg_prem:Q')
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['avg'])
).project(
    type='albersUsa'
).properties(
    width=700,
    height=450
) 

这里是第二张地图的代码:

# US states background
alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).properties(
    title='US State Capitols',
    width=700,
    height=400
).project('albersUsa')

我尝试的主要内容是应用第一张地图上第二张地图中的填充和笔触参数:

alt.Chart(states).mark_geoshape(fill='lightgray',
    stroke='white').encode(
    color=alt.Color('avg_prem:Q')
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['avg'])
).project(
    type='albersUsa'
).properties(
    width=700,
    height=450
) 

我可以用这种方式更改具有值的州的轮廓的颜色,但不能用空值填充州。

有没有修复地图上丢失数据问题的好方法?

推荐答案

一种方法是使用具有所需背景的分层图表。您没有提供数据,因此我无法实际尝试,但可能如下所示:

states = alt.topo_feature(data.us_10m.url, 'states')
source = df

foreground = alt.Chart(states).mark_geoshape().encode(
    color=alt.Color('avg_prem:Q')
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['avg'])
).project(
    type='albersUsa'
).properties(
    width=700,
    height=400
)  

background = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).properties(
    title='US State Capitols',
    width=700,
    height=400
).project('albersUsa')

background + foreground

编辑:另一种可能的方法是使用条件编码,类似于https://vega.github.io/vega-lite/examples/point_invalid_color.html

alt.Chart(states).mark_geoshape().encode(
    color=alt.condition('datum.avg_prem !== null', 'avg_prem:Q', alt.value('lightgray'))
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['avg'])
).project(
    type='albersUsa'
).properties(
    width=700,
    height=400
)  

这篇关于牛郎星全息图中缺失值/空值的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆