如何在牛郎星上制作“小倍数”(map)图表

2024-09-29 17:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个小的倍数图;像这样的

small multiples map

带着地球仪和牵牛星。“我的数据集”包含以下字段:

  • 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

Tags: 数据parketcaltqueenschartspolygonhill
1条回答
网友
1楼 · 发布于 2024-09-29 17:19:18

听起来你想要一个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
)

enter image description here

相关问题 更多 >

    热门问题