Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raster.show() does not respect the current axis. #241

Closed
erikmannerfelt opened this issue Aug 17, 2021 · 2 comments · Fixed by #351
Closed

Raster.show() does not respect the current axis. #241

erikmannerfelt opened this issue Aug 17, 2021 · 2 comments · Fixed by #351
Labels
invalid This doesn't seem right

Comments

@erikmannerfelt
Copy link
Contributor

Describe the bug
The Raster.show() method does not care about subplots, and also calls plt.show() which messes up any attempt to fix it! The easy solution is to add the ax keyword, but it is odd that ax does not default to plt.gca().

To Reproduce
Steps to reproduce the behavior:

import matplotlib.pyplot as plt
import geoutils as gu

img1 = gu.Raster(gu.datasets.get_path("landsat_B4"))
img2 = img1 + 10


plt.subplot(121)
img1.show()
plt.subplot(122)
img2.show()

plt.show()

This is how to make it work correctly, but it is not easy for newcomers to understand why:

import matplotlib.pyplot as plt
import geoutils as gu

img1 = gu.Raster(gu.datasets.get_path("landsat_B4"))
img2 = img1 + 10


ax0 = plt.subplot(121)
img1.show(ax=ax0)
ax1 = plt.subplot(122)
img2.show(ax=ax1)

plt.show()

Expected behavior
One figure with two subplots should be created. This currently shows four plots; two empty subplots and two single raster plots.

@erikmannerfelt erikmannerfelt added bug Something isn't working invalid This doesn't seem right labels Aug 17, 2021
@adehecq
Copy link
Member

adehecq commented Aug 25, 2021

You're right, it's not ideal. Should we just copy the behavior of rasterio? Which is:

  • if no ax is provided, use plt.gca() and is followed by plt.show()
  • otherwise, use provided ax and does not call plt.show()

https://github.com/mapbox/rasterio/blob/3910956d6cfadd55ea085dd60790246c167967cd/rasterio/plot.py#L130

I like that plt.show() is called when no argument is given to img.show() so that one get a view of the raster in one line. But I think that would still prevent your first example unfortunately...

For info, it seems like geopandas also completely ignore the current axes too (so we can do better! 😉):

vect = gu.Vector(gu.datasets.get_path("glacier_outlines"))
plt.subplot(121)
vect.ds.plot()
plt.subplot(122)
vect.ds.plot()
plt.show()

@atedstone
Copy link
Member

Incidentally, our current behaviour is far closer to that of seaborn and geopandas. Both of these seem to always ignore the current axes, you have to pass ax=myax to explicitly use an existing axis.

In any case, perhaps this isn't really a bug but more of a design choice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants