如何在 altair python 中将列中的数据绘制到世界地图上

如何解决如何在 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 在世界地图上绘制信息以获得如下内容

enter image description here

(注意:我只需要一张世界地图 - 而不是图片中的两张)

我的意图是 - 将总列作为红点(根据数字的大小),悬停在上面时会显示(总和)的值。在侧面 - 最好是右侧或顶部,我想为年份,排名和房屋创建一个选择器(下拉菜单),并带有一个交互选项 -

例如。在法国 - 如果我选择 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

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?