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


基础教程推荐
猜你喜欢
- 将 YAML 文件转换为 python dict 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01