Dealing with missing values / nulls in Altair choropleth map(牛郎星全息图中缺失值/空值的处理)
本文介绍了牛郎星全息图中缺失值/空值的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经用美国州级数据在牛郎星创建了一张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
)
这篇关于牛郎星全息图中缺失值/空值的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:牛郎星全息图中缺失值/空值的处理
基础教程推荐
猜你喜欢
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
