Skip to content

Commit

Permalink
BUG: do not change the aspect of the axis (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfleis authored Aug 31, 2024
1 parent 0e38541 commit 00ca031
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
21 changes: 16 additions & 5 deletions contextily/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def add_basemap(
crs=None,
resampling=Resampling.bilinear,
zoom_adjust=None,
**extra_imshow_args
**extra_imshow_args,
):
"""
Add a (web/local) basemap to `ax`.
Expand Down Expand Up @@ -78,7 +78,7 @@ def add_basemap(
`rasterio.enums.Resampling` method
zoom_adjust : int or None
[Optional. Default: None]
The amount to adjust a chosen zoom level if it is chosen automatically.
The amount to adjust a chosen zoom level if it is chosen automatically.
Values outside of -1 to 1 are not recommended as they can lead to slow execution.
**extra_imshow_args :
Other parameters to be passed to `imshow`.
Expand Down Expand Up @@ -132,7 +132,14 @@ def add_basemap(
)
# Download image
image, extent = bounds2img(
left, bottom, right, top, zoom=zoom, source=source, ll=False, zoom_adjust=zoom_adjust
left,
bottom,
right,
top,
zoom=zoom,
source=source,
ll=False,
zoom_adjust=zoom_adjust,
)
# Warping
if crs is not None:
Expand Down Expand Up @@ -190,8 +197,12 @@ def add_basemap(
# Plotting
if image.shape[2] == 1:
image = image[:, :, 0]
img = ax.imshow(
image, extent=extent, interpolation=interpolation, **extra_imshow_args
_ = ax.imshow(
image,
extent=extent,
interpolation=interpolation,
aspect=ax.get_aspect(), # GH251
**extra_imshow_args,
)

if reset_extent:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cx.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,24 @@ def test_set_cache_dir(tmpdir):
fig, ax = matplotlib.pyplot.subplots()
ax.axis(extent)
cx.add_basemap(ax)


@pytest.mark.network
def test_aspect():
"""Test that contextily does not change set aspect"""
# Plot boulder bbox as in test_place
x1, x2, y1, y2 = [
-11740727.544603072,
-11701591.786121061,
4852834.0517692715,
4891969.810251278,
]

# Test web basemap
fig, ax = matplotlib.pyplot.subplots(1)
ax.set_xlim(x1, x2)
ax.set_ylim(y1, y2)
ax.set_aspect(2)
cx.add_basemap(ax, zoom=10)

assert ax.get_aspect() == 2

0 comments on commit 00ca031

Please sign in to comment.