Vega-Lite / Altair:如何居中或裁剪欧洲地图?

Vega-Lite / Altair: How to Center or Crop a Map of Europe?

我有一些关于欧洲国家的数据。我正在尝试使用 world-110m 数据在 Altair / Vega-Lite 中创建可视化。技术上一切正常,除了国家的编码边界还包括遥远的领土,产生了一个看起来像这样的糟糕地图:

这是我的代码:

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

map = alt.Chart(countries).mark_geoshape(
    stroke='black'
    ).encode(
    color=alt.Color('SomeStat:Q', sort="descending", scale=alt.Scale(
        scheme='inferno', domain=(min_value,max_value)), legend=alt.Legend(title="", tickCount=6))
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'CountryId', ['SomeStat', 'CountryName'])
).project(
    type='mercator'
)

有没有办法裁剪这张地图或将其居中,以便我只显示欧洲而不是世界各地的偏远地区?

或者,是否有更好的 public 数据集我应该使用,它只包括欧洲?

我没有你的 df 数据集,所以我 post 比较简单的例子。

import altair as alt
from vega_datasets import data

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

alt.Chart(countries).mark_geoshape(
    fill='#666666',
    stroke='white'
).project(
    type= 'mercator',
    scale= 350,                          # Magnify
    center= [20,50],                     # [lon, lat]
    clipExtent= [[0, 0], [400, 300]],    # [[left, top], [right, bottom]]
).properties(
    title='Europe (Mercator)',
    width=400, height=300
)

您可以通过 scalecenter 以及其实际绘图大小(widthheight)控制地图视图。

  • scale:放大参数
  • center: 视图的中心点

如果您需要进一步裁剪地图的任何部分,clipExtent 会很有用。请注意 - 此数组代表像素大小,而不是地理坐标。 (在上面的示例中,我将其设置为 [[0, 0], [400, 300]],因此它保留了整个 400x300 px 视图。