From 00ca0318033e69e29a164ce571d9c932a057ecc6 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Sat, 31 Aug 2024 17:21:09 +0200 Subject: [PATCH] BUG: do not change the aspect of the axis (#253) --- contextily/plotting.py | 21 ++++++++++++++++----- tests/test_cx.py | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/contextily/plotting.py b/contextily/plotting.py index bd711e33..ec14d6cd 100644 --- a/contextily/plotting.py +++ b/contextily/plotting.py @@ -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`. @@ -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`. @@ -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: @@ -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: diff --git a/tests/test_cx.py b/tests/test_cx.py index 5566b3f0..01c7022e 100644 --- a/tests/test_cx.py +++ b/tests/test_cx.py @@ -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