如何在 Altair 中制作 "small multiples"(地图)图表
How to make "small multiples" (map) chart in Altair
我正在尝试制作一个小倍数地图图表;像这样
使用 GeoPandas 和 Altair。我的数据集包含以下字段:
weekNumber
percentageDiff
geometry
我想为每个 weekNumber
创建一个地图,显示 percentageDiff
列中的值。
我已经成功地在一行中制作了多张地图,但不是像这一张这样的网格。这是我的一行地图代码:
charts = []
for x in range(12):
chart = alt.Chart(ntaGeoData[ntaGeoData.weekNumber == x]).mark_geoshape().encode(
color=alt.Color(
'percentageDiff:Q',
scale=alt.Scale(scheme='redblue',domain=(-1,1)))
).properties(
width=200,
height=200
)
charts.append(chart)
alt.hconcat(*charts)
有什么想法吗?谢谢!
实际数据是这样的:
geometry weekNumber citiStartDiff NTAName
0 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 0 -0.319463 Brooklyn Heights-Cobble Hill
1 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 1 0.232122 Brooklyn Heights-Cobble Hill
2 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 2 0.101468 Brooklyn Heights-Cobble Hill
3 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 3 0.191144 Brooklyn Heights-Cobble Hill
4 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 4 0.378864 Brooklyn Heights-Cobble Hill
... ... ... ... ...
6147 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 4 2.520000 park-cemetery-etc-Queens
6148 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 5 0.030769 park-cemetery-etc-Queens
6149 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 6 -0.015625 park-cemetery-etc-Queens
6150 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 7 1.150000 park-cemetery-etc-Queens
6151 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 8 0.509804 park-cemetery-etc-Queens
听起来你想要 wrapped facet。它看起来像这样:
alt.Chart(ntaGeoData).mark_geoshape().encode(
color=alt.Color(
'percentageDiff:Q',
scale=alt.Scale(scheme='redblue',domain=(-1,1))),
facet=alt.Facet('weekNumber:O', columns=4)
).properties(
width=200,
height=200
)
不幸的是,由于 Vega-Lite 中的 bug,这种分面不适用于地理可视化。作为解决方法,您可以手动过滤 pandas 中的数据,并通过串联创建一个小的倍数图表。例如:
alt.concat(*(
alt.Chart(ntaGeoData[ntaGeoData.weekNumber == weekNumber]).mark_geoshape().encode(
color='citiStartDiff:Q',
).properties(
width=200, height=200
)
for weekNumber in range(8)
), columns=4
)
我正在尝试制作一个小倍数地图图表;像这样
使用 GeoPandas 和 Altair。我的数据集包含以下字段:
weekNumber
percentageDiff
geometry
我想为每个 weekNumber
创建一个地图,显示 percentageDiff
列中的值。
我已经成功地在一行中制作了多张地图,但不是像这一张这样的网格。这是我的一行地图代码:
charts = []
for x in range(12):
chart = alt.Chart(ntaGeoData[ntaGeoData.weekNumber == x]).mark_geoshape().encode(
color=alt.Color(
'percentageDiff:Q',
scale=alt.Scale(scheme='redblue',domain=(-1,1)))
).properties(
width=200,
height=200
)
charts.append(chart)
alt.hconcat(*charts)
有什么想法吗?谢谢!
实际数据是这样的:
geometry weekNumber citiStartDiff NTAName
0 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 0 -0.319463 Brooklyn Heights-Cobble Hill
1 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 1 0.232122 Brooklyn Heights-Cobble Hill
2 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 2 0.101468 Brooklyn Heights-Cobble Hill
3 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 3 0.191144 Brooklyn Heights-Cobble Hill
4 POLYGON ((-73.99236 40.68969, -73.99436 40.690... 4 0.378864 Brooklyn Heights-Cobble Hill
... ... ... ... ...
6147 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 4 2.520000 park-cemetery-etc-Queens
6148 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 5 0.030769 park-cemetery-etc-Queens
6149 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 6 -0.015625 park-cemetery-etc-Queens
6150 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 7 1.150000 park-cemetery-etc-Queens
6151 MULTIPOLYGON (((-73.86523 40.57046, -73.86454 ... 8 0.509804 park-cemetery-etc-Queens
听起来你想要 wrapped facet。它看起来像这样:
alt.Chart(ntaGeoData).mark_geoshape().encode(
color=alt.Color(
'percentageDiff:Q',
scale=alt.Scale(scheme='redblue',domain=(-1,1))),
facet=alt.Facet('weekNumber:O', columns=4)
).properties(
width=200,
height=200
)
不幸的是,由于 Vega-Lite 中的 bug,这种分面不适用于地理可视化。作为解决方法,您可以手动过滤 pandas 中的数据,并通过串联创建一个小的倍数图表。例如:
alt.concat(*(
alt.Chart(ntaGeoData[ntaGeoData.weekNumber == weekNumber]).mark_geoshape().encode(
color='citiStartDiff:Q',
).properties(
width=200, height=200
)
for weekNumber in range(8)
), columns=4
)