Skip to content

Commit 00ca031

Browse files
authored
BUG: do not change the aspect of the axis (#253)
1 parent 0e38541 commit 00ca031

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

contextily/plotting.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def add_basemap(
2626
crs=None,
2727
resampling=Resampling.bilinear,
2828
zoom_adjust=None,
29-
**extra_imshow_args
29+
**extra_imshow_args,
3030
):
3131
"""
3232
Add a (web/local) basemap to `ax`.
@@ -78,7 +78,7 @@ def add_basemap(
7878
`rasterio.enums.Resampling` method
7979
zoom_adjust : int or None
8080
[Optional. Default: None]
81-
The amount to adjust a chosen zoom level if it is chosen automatically.
81+
The amount to adjust a chosen zoom level if it is chosen automatically.
8282
Values outside of -1 to 1 are not recommended as they can lead to slow execution.
8383
**extra_imshow_args :
8484
Other parameters to be passed to `imshow`.
@@ -132,7 +132,14 @@ def add_basemap(
132132
)
133133
# Download image
134134
image, extent = bounds2img(
135-
left, bottom, right, top, zoom=zoom, source=source, ll=False, zoom_adjust=zoom_adjust
135+
left,
136+
bottom,
137+
right,
138+
top,
139+
zoom=zoom,
140+
source=source,
141+
ll=False,
142+
zoom_adjust=zoom_adjust,
136143
)
137144
# Warping
138145
if crs is not None:
@@ -190,8 +197,12 @@ def add_basemap(
190197
# Plotting
191198
if image.shape[2] == 1:
192199
image = image[:, :, 0]
193-
img = ax.imshow(
194-
image, extent=extent, interpolation=interpolation, **extra_imshow_args
200+
_ = ax.imshow(
201+
image,
202+
extent=extent,
203+
interpolation=interpolation,
204+
aspect=ax.get_aspect(), # GH251
205+
**extra_imshow_args,
195206
)
196207

197208
if reset_extent:

tests/test_cx.py

+21
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,24 @@ def test_set_cache_dir(tmpdir):
660660
fig, ax = matplotlib.pyplot.subplots()
661661
ax.axis(extent)
662662
cx.add_basemap(ax)
663+
664+
665+
@pytest.mark.network
666+
def test_aspect():
667+
"""Test that contextily does not change set aspect"""
668+
# Plot boulder bbox as in test_place
669+
x1, x2, y1, y2 = [
670+
-11740727.544603072,
671+
-11701591.786121061,
672+
4852834.0517692715,
673+
4891969.810251278,
674+
]
675+
676+
# Test web basemap
677+
fig, ax = matplotlib.pyplot.subplots(1)
678+
ax.set_xlim(x1, x2)
679+
ax.set_ylim(y1, y2)
680+
ax.set_aspect(2)
681+
cx.add_basemap(ax, zoom=10)
682+
683+
assert ax.get_aspect() == 2

0 commit comments

Comments
 (0)