How to make "small multiples" (map) chart in Altair

I'm trying to make a small multiples map chart; something like this

small multiples map

with GeoPandas and Altair. My dataset has the following fields:

I want to create a map for each weekNumber showing the values in the percentageDiff column.

I've managed to make multiple maps on one row, but not in a grid like this one. Here's my code for the maps on one row:

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)

Any ideas? Thank you!

The actual data looks like this:

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

Upvotes: 2

Views: 1386

Answers (1)

jakevdp
jakevdp

Reputation: 86300

It sounds like you want a wrapped facet. It would look something like this:

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
)

Unfortunately, due to a bug in Vega-Lite, this kind of faceting does not work for geographic visualization. As a workaround, you can manually filter the data in pandas, and create a small multiples chart via concatenation. For example:

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

Upvotes: 4

Related Questions