如何解决如何在 altair python 中将列中的数据绘制到世界地图上
对于可视化项目,我有以下(示例)数据集:
country|continent|rank|house|total|year
france|Eu |a |top |23 |2010
france|Eu |b |top |21 |2010
france|Eu |b |mid |19 |2012
italy |Eu |b |top |25 |2010
italy |Eu |a |top |26 |2012
china |asia |b |mid |35 |2010
korea |asia |a |mid |29 |2010
china |asia |a |top |40 |2012
korea |asia |b |top |33 |2012
kenya |africa |b |mid |20 |2010
kenya |africa |c |mid |18 |2012
我想使用 altair 在世界地图上绘制信息以获得如下内容:
(注意:我只需要一张世界地图 - 而不是图片中的两张)
我的意图是 - 将总列作为红点(根据数字的大小),悬停在上面时会显示(总和)的值。在侧面 - 最好是右侧或顶部,我想为年份,排名和房屋创建一个选择器(下拉菜单),并带有一个交互选项 -
例如。在法国 - 如果我选择 house - top,则显示总数应为 44,而在选择 house-mid 时,总数应为 19。 作为最后一步 - 我想要大陆选择器下拉菜单,这样如果选择了一个大陆,该部分就会放大。
我最近开始使用 altair,我知道如何构建单独的组件,例如下拉列表或连接两个图表等。至于世界地图 -
import altair as alt
from vega_datasets import data
# Data generators for the background
sphere = alt.sphere()
graticule = alt.graticule()
# Source of land data
source = alt.topo_feature(data.world_110m.url,'countries')
# Layering and configuring the components
alt.layer(
alt.Chart(sphere).mark_geoshape(fill='lightblue'),alt.Chart(graticule).mark_geoshape(stroke='white',strokeWidth=0.5),alt.Chart(source).mark_geoshape(fill='ForestGreen',stroke='black')
).project(
'naturalEarth1'
).properties(width=600,height=400).configure_view(stroke=None)
它是 altair 文档中的代码示例,但我真的不知道如何实现我的目标。有人可以帮我吗。
解决方法
华盛顿大学有一个 good intro notebook for Altair geo plotting。对于您的情况,您可以plot the map and the scatter in two different charts and combine them as in this example。
import altair as alt
from vega_datasets import data
airports = data.airports.url
states = alt.topo_feature(data.us_10m.url,feature='states')
# US states background
background = alt.Chart(states).mark_geoshape(
fill='lightgray',stroke='white'
).properties(
width=500,height=300
).project('albersUsa')
# airport positions on background
points = alt.Chart(airports).transform_aggregate(
latitude='mean(latitude)',longitude='mean(longitude)',count='count()',groupby=['state']
).mark_circle().encode(
longitude='longitude:Q',latitude='latitude:Q',size=alt.Size('count:Q',title='Number of Airports'),color=alt.value('steelblue'),tooltip=['state:N','count:Q']
).properties(
title='Number of airports in US'
)
background + points
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。