Concatenate two geoshape charts in Altair / Vega-Lite with independent axis?

  Kiến thức lập trình

I am trying to plot two separate geoshape charts and concatenate them together. Below is a MWE of what I am trying to do:

import altair as alt
from vega_datasets import data
import geopandas as gpd

source = data.us_10m

source = gpd.read_file(source.url).set_crs(epsg=4326)

r1 = alt.Chart(source).mark_geoshape().transform_filter(alt.datum.id == '49047')
r2 = alt.Chart(source).mark_geoshape().transform_filter(alt.datum.id == '20123')

r1 | r2

Plotting them separately works great; Altair plots the shapes and zooms to their extents automatically.

R1:

Region 1 Chart

R2:

Region 2 Chart

However, when I try to concatenate them, they share axis limits, and each chart’s view expands large enough to show both even if only one is displayed.

Concatenated Plot:

Concatenated Plot

My expectation is that I get the R1 and R2 images above next to each other without the additional zooming. I tried to set the axis to resolve independently, but it did not change the output.

(r1 | r2).resolve_axis(x='independent', y='independent')

Answers for how to do this in Vega-Lite, without the Altair abstraction, would also be useful.

LEAVE A COMMENT