【问题标题】:How to plot the data from the columns onto a world map in altair python如何在altair python中将列中的数据绘制到世界地图上
【发布时间】:2021-01-01 09:16:07
【问题描述】:

对于一个可视化项目,我有以下(示例)数据集:

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 文档中的代码示例,但我真的不知道如何实现我的目标。谁能帮帮我。

【问题讨论】:

    标签: javascript python data-visualization altair vega-lite


    【解决方案1】:

    华盛顿大学有一个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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多