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="VVMgU3RhdGUgQ2FwaXRvbHM=",
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="VVMgU3RhdGUgQ2FwaXRvbHM=",
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
)
这篇关于牛郎星全息图中缺失值/空值的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:牛郎星全息图中缺失值/空值的处理


猜你喜欢
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 沿轴计算直方图 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01