79653580

Date: 2025-06-04 22:32:22
Score: 0.5
Natty:
Report link

I'm not sure if this exactly addressed your question, but I'll mention how I manipulate the basemap's size/zoom level when I'm using contextily

For the sake of the example, have a geodataframe of train stops in San Francisco:

print(bart_stops)
                     stop_name                           geometry
16739    16th Street / Mission   POINT (-13627704.79 4546305.962)
16740    24th Street / Mission  POINT (-13627567.088 4544510.141)
16741              Balboa Park  POINT (-13630794.462 4540193.774)
16742  Civic Center / UN Plaza  POINT (-13627050.676 4548316.174)
16744              Embarcadero   POINT (-13625180.62 4550195.061)
16745                Glen Park  POINT (-13629241.221 4541813.891)
16746        Montgomery Street   POINT (-13625688.46 4549691.325)
16747            Powell Street   POINT (-13626327.99 4549047.884)

and I want to plot that over a contextily basemap of all of San Francisco. However, if I just add the basemap, I get a plot where the basemap is zoomed into the points -- you can't see the rest of the geography. No matter what I do to figsize it will not change.

fig, ax = plt.subplots(figsize=(5, 5))
bart_stops.plot(ax=ax, markersize=9, column='agency', marker="D")
cx.add_basemap(ax, source=cx.providers.CartoDB.VoyagerNoLabels, crs=bart_stops.crs)
ax.axis("off")
fig.tight_layout();

enter image description here

To get around this, I manipulate the xlim and ylim of the plot, by referencing another geodataframe with a polygon of the area I'm interested in (I would get that using pygris in the U.S. to get census shapefiles-- I'm less familiar with the options in other countries). in this case I have the following geodataframe with the multipolygon of San Francsico.

print(sf_no_water_web_map)
                   region                                           geometry
0  San Francisco Bay Area  MULTIPOLYGON (((-13626865.552 4538318.942, -13...

plotted together with the train stops, they look like this:

fig, ax = plt.subplots(figsize=(5, 5))
sf_no_water_web_map.plot(ax=ax, facecolor="none")
bart_stops.plot(ax=ax);

enter image description here

With that outline of the city sf_no_water_web_map, I can set the xlim and ylim of a plot -- even when I don't explicitly plot that geodataframe -- by passing its bounds into the axis of the plot.

fig, ax = plt.subplots(figsize=(5, 5))
bart_stops.plot(ax=ax, markersize=9, column='agency', marker="D")
# Use another shape to determine the zoom/map size
assert sf_no_water_web_map.crs == bart_stops.crs
sf_bounds = sf_no_water_web_map.bounds.iloc[0]
ax.set(xlim = (sf_bounds['minx'], sf_bounds['maxx']),
       ylim = (sf_bounds['miny'], sf_bounds['maxy'])
       )
ax.axis("off")
fig.tight_layout()
cx.add_basemap(ax, source=cx.providers.CartoDB.VoyagerNoLabels, crs=bart_stops.crs)

enter image description here

Hopefully that connects to your desire to re-size the basemap.

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Peter