Python 3.x 如何更改altair(python vega lite)中geo_形状的限制
我试图用python和Altair绘制美国三个州的位置。我看过关于美国地图的教程,但我想知道是否有任何方法可以将图像缩放到仅感兴趣的三个状态,即纽约、新泽西和CT 目前,我有以下代码:Python 3.x 如何更改altair(python vega lite)中geo_形状的限制,python-3.x,geojson,vega-lite,altair,Python 3.x,Geojson,Vega Lite,Altair,我试图用python和Altair绘制美国三个州的位置。我看过关于美国地图的教程,但我想知道是否有任何方法可以将图像缩放到仅感兴趣的三个状态,即纽约、新泽西和CT 目前,我有以下代码: from vega_datasets import data states = alt.topo_feature(data.us_10m.url, 'states') # US states background background = alt.Chart(states).mark_g
from vega_datasets import data
states = alt.topo_feature(data.us_10m.url, 'states')
# US states background
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white',
limit=1000
).properties(
title='US State Capitols',
width=700,
height=400
).project("albers")
points=alt.Chart(accts).mark_point().encode(
longitude = "longitude",
latitude = "latitude",
color = "Group")
background+points
我检查了us_10m.url数据集,似乎没有指定各个状态的字段。所以我希望我能把背景的xlim和ylim改成[-80,-70]和[35,45]。我想放大到有数据点(蓝点)的区域
有人能告诉我怎么做吗?谢谢
更新
JSON文件中有一个名为ID的字段,我手动发现NJ是34,NY是36,CT是9。有没有办法过滤这些ID?那会完成任务的 好的,似乎还不支持geotype的选择/缩放/xlim/ylim功能: 因此,我最终采用了一种黑客方法来解决这个问题,首先使用纯python基于ID进行过滤。基本上,将JSON文件加载到字典中,然后在将字典转换为JSON格式之前更改值字段。下面是5个州的示例,PA、NJ、NY、CT、RI和MA
import altair as alt
from vega_datasets import data
# Load the data, which is loaded as a dict object
us_10m = data.us_10m()
# Select the geometries under states under objects, filter on id (9,25,34,36,42,44)
us_10m['objects']['states']['geometries']=[item for item in us_10m['objects'] \
['states']['geometries'] if item['id'] in [9,25,34,36,42,44]]
# Make the topojson data
states = alt.Data(
values=us_10m,
format=alt.TopoDataFormat(feature='states',type='topojson'))
# Plot background (now only has 5 states)
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white',
limit=1000
).properties(
title='US State Capitols',
width=700,
height=400
).project("mercator")
# Plot the points
points=alt.Chart(accts).mark_circle(size=60).encode(
longitude = "longitude",
latitude = "latitude",
color = "Group").project("mercator")
# Overlay the two plots
background+points
生成的绘图看起来正常: