From 899b738898099ff63d40a7de81d3cda35798f5fd Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sat, 21 Jul 2018 13:41:45 +0200 Subject: [PATCH 1/3] add_basemap: keep original axis limits of figure --- contextily/plotting.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/contextily/plotting.py b/contextily/plotting.py index 2a2396d7..216d7991 100644 --- a/contextily/plotting.py +++ b/contextily/plotting.py @@ -66,11 +66,11 @@ def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN, >>> plt.show() ''' + xmin, xmax, ymin, ymax = ax.axis() # If web source if url[:4] == 'http': # Extent - left, right = ax.get_xlim() - bottom, top = ax.get_ylim() + left, right, bottom, top = xmin, xmax, ymin, ymax # Zoom if isinstance(zoom, str) and (zoom.lower() == 'auto'): min_ll = _sm2ll(left, bottom) @@ -90,5 +90,6 @@ def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN, # Plotting ax.imshow(image, extent=extent, interpolation=interpolation, **extra_imshow_args) + ax.axis((xmin, xmax, ymin, ymax)) return ax From d7cb4c02ce21e3cf08ddc7a26d13f549c08195f3 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 30 Nov 2018 20:15:07 +0100 Subject: [PATCH 2/3] add test --- tests/test_ctx.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_ctx.py b/tests/test_ctx.py index 1d4e5630..3ff753d5 100644 --- a/tests/test_ctx.py +++ b/tests/test_ctx.py @@ -156,9 +156,11 @@ def test_add_basemap(): ax.set_ylim(y1, y2) ax = ctx.add_basemap(ax, zoom=10) - ax_extent = (-11740727.544603072, -11662456.027639052, - 4852834.0517692715, 4891969.810251278) - assert_array_almost_equal(ax_extent, ax.images[0].get_extent()) + ax_extent = (x1, x2, y1, y2) + + # ensure add_basemap did not change the axis limits of ax + assert ax.axis() == ax_extent + assert ax.images[0].get_array().sum() == 75687792 assert ax.images[0].get_array().shape == (256, 511, 3) assert_array_almost_equal(ax.images[0].get_array().mean(), From e5fc902f0db373449bec131ad8453dad4a6529ae Mon Sep 17 00:00:00 2001 From: Dani Arribas-Bel Date: Fri, 19 Apr 2019 18:55:15 -0500 Subject: [PATCH 3/3] Adding reset_extent=True argument to add_basemap to give users the option to reset extent or not --- contextily/plotting.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/contextily/plotting.py b/contextily/plotting.py index 75d85e27..b316b1e6 100644 --- a/contextily/plotting.py +++ b/contextily/plotting.py @@ -12,7 +12,7 @@ def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN, interpolation=INTERPOLATION, attribution = ATTRIBUTION, - **extra_imshow_args): + reset_extent=True, **extra_imshow_args): """ Add a (web/local) basemap to `ax` ... @@ -38,6 +38,10 @@ def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN, attribution : str [Optional. Defaults to standard `ATTRIBUTION`] Text to be added at the bottom of the axis. + reset_extent : Boolean + [Optiona. Default=True] If True, the extent of the + basemap added is reset to the original extent (xlim, + ylim) of `ax` **extra_imshow_args : dict Other parameters to be passed to `imshow`. @@ -94,7 +98,8 @@ def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN, ax.imshow(image, extent=extent, interpolation=interpolation, **extra_imshow_args) - ax.axis((xmin, xmax, ymin, ymax)) + if reset_extent is True: + ax.axis((xmin, xmax, ymin, ymax)) if attribution: add_attribution(ax, attribution)