地理图

Altair 2.0 添加了绘制地理数据的能力。

此功能尚有些不完善(例如,并非所有交互或选择都能在投影数据上正常工作),但使用起来相对简单。

我们将在这里展示几个示例。

import altair as alt

地理坐标中的散点图

首先,我们将展示使用地图投影绘制经纬度数据的示例。我们将加载包含美国所有机场经纬度的数据集。

from vega_datasets import data
airports = data.airports()
airports.head()
iata name city state country latitude longitude
0 00M Thigpen Bay Springs MS USA 31.953765 -89.234505
1 00R Livingston Municipal Livingston TX USA 30.685861 -95.017928
2 00V Meadow Lake Colorado Springs CO USA 38.945749 -104.569893
3 01G Perry-Warsaw Perry NY USA 42.741347 -78.052081
4 01J Hilliard Airpark Hilliard FL USA 30.688012 -81.905944

该图表与标准散点图非常相似,但有一些区别

  • 我们使用“latitude”和“longitude”编码来代替“x”和“y”

  • 我们指定用于数据的投影

对于仅覆盖美国的数据,"albersUsa" 投影很有用

alt.Chart(airports).mark_circle().encode(
    longitude='longitude:Q',
    latitude='latitude:Q',
    size=alt.value(10),
    tooltip='name'
).project(
    "albersUsa"
).properties(
    width=500,
    height=400
)

可用的投影列在 vega 文档中。

分级统计图

如果您想绘制地理边界,例如州和国家,您必须加载地理形状数据才能在 Altair 中显示。这需要一些样板代码(我们正在考虑如何在未来的版本中简化这种常见结构),并使用 geoshape 标记。

例如,这是州边界

states = alt.topo_feature(data.us_10m.url, feature='states')

alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).project('albersUsa').properties(
    width=500,
    height=300
)

这是世界国家边界

import altair as alt
from vega_datasets import data

countries = alt.topo_feature(data.world_110m.url, 'countries')

alt.Chart(countries).mark_geoshape(
    fill='lightgray',
    stroke='white'
).project(
    "equirectangular"
).properties(
    width=500,
    height=300
)

您可以看看尝试其他投影类型时会发生什么;例如,您可以尝试“mercator”、“orthographic”、“albers”或“gnomonic”。

背景上的点

如果您想在地图背景上绘制数据点,最简单的方法是使用我们在 04-Compound-charts 中看到的叠加操作符。例如

states = alt.topo_feature(data.us_10m.url, feature='states')
airports = data.airports()

background = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).project('albersUsa').properties(
    width=500,
    height=300
)

points = alt.Chart(airports).mark_circle().encode(
    longitude='longitude:Q',
    latitude='latitude:Q',
    size=alt.value(10),
    tooltip='name'
)

background + points

注意,我们只需要指定一次投影和图表大小

带颜色的分级统计图

最复杂的图表类型是地图区域根据底层数据着色的图表;之所以复杂,是因为它通常涉及使用 查找转换(lookup transform) 来连接两个不同的数据集。

同样,这是我们希望在未来改进的 API 部分。

例如,这是一个表示各州总人口的图表

pop = data.population_engineers_hurricanes()
pop.head()
state id population engineers hurricanes
0 Alabama 1 4863300 0.003422 22
1 Alaska 2 741894 0.001591 0
2 Arizona 4 6931071 0.004774 0
3 Arkansas 5 2988248 0.002440 0
4 California 6 39250017 0.007126 0
import altair as alt
from vega_datasets import data

states = alt.topo_feature(data.us_10m.url, 'states')

variable_list = ['population', 'engineers', 'hurricanes']

alt.Chart(states).mark_geoshape().encode(
    color='population:Q'
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(pop, 'id', list(pop.columns))
).properties(
    width=500,
    height=300
).project(
    type='albersUsa'
)

请注意这里的关键:分级统计图数据有一个“id”列,它对应于人口数据中的 id 列;我们将其用作查找键,将两个数据集连接在一起并适当地绘制它们。

有关地理可视化工具的更多示例,请参见 altair 图库,请记住这是 Altair 和 Vega-Lite 中正在稳步改进的领域!