corteva / rioxarray

geospatial xarray extension powered by rasterio
https://corteva.github.io/rioxarray
Other
517 stars 81 forks source link

clipped.plot() hangs with <matplotlib.collections.QuadMesh at 0x158cbf1f0> #268

Closed robmarkcole closed 3 years ago

robmarkcole commented 3 years ago

I have created a clipped object following the example from the docs which I can plot with matplotlib but which hangs when calling the plot method on the object.

import rioxarray
import geopandas as gpd
from shapely.geometry import box

url = 'my cog url'
ds = rioxarray.open_rasterio(url).squeeze() # squeeze as single band

# Get valid bounding box to clip 
minx, miny, maxx, maxy = ds.rio.bounds()
bbox_clip = box(minx, miny, (minx + maxx)/2, (miny+maxy)/2)

clip_df = gpd.GeoDataFrame(
    geometry=[
        bbox_clip
    ],
    crs="EPSG:4326"
)

clipped = ds.rio.clip(clip_df.geometry, clip_df.crs, drop=False, invert=False)
clipped.plot() # hangs with <matplotlib.collections.QuadMesh at 0x158cbf1f0>

However I can plot with:

import matplotlib.pyplot as plt
plt.imshow(clipped.data)

Environment Information

Installation method

pypi

environment information

affine==2.3.0
anyio==2.1.0
appnope==0.1.2
argon2-cffi==20.1.0
async-generator==1.10
attrs==20.3.0
Babel==2.9.0
backcall==0.2.0
bleach==3.3.0
boto3==1.17.16
botocore==1.20.16
branca==0.4.2
certifi==2020.12.5
cffi==1.14.5
chardet==4.0.0
click==7.1.2
click-plugins==1.1.1
cligj==0.7.1
cycler==0.10.0
dask==2021.2.0
decorator==4.4.2
defusedxml==0.6.0
descartes==1.1.0
entrypoints==0.3
et-xmlfile==1.0.1
Fiona==1.8.18
folium==0.12.1
fsspec==0.8.7
geopandas==0.8.2
gpxpy==1.4.2
idna==2.10
imageio==2.9.0
iniconfig==1.1.1
ipykernel==5.5.0
ipython==7.20.0
ipython-genutils==0.2.0
jdcal==1.4.1
jedi==0.18.0
Jinja2==2.11.3
jmespath==0.10.0
json5==0.9.5
jsonschema==3.2.0
jupyter-client==6.1.11
jupyter-core==4.7.1
jupyter-packaging==0.7.12
jupyter-server==1.4.1
jupyterlab-pygments==0.1.2
jupyterlab-server==2.3.0
kiwisolver==1.3.1
MarkupSafe==1.1.1
matplotlib==3.3.4
mistune==0.8.4
morecantile==2.1.0
munch==2.5.0
nbclassic==0.2.6
nbclient==0.5.3
nbconvert==6.0.7
nbformat==5.1.2
nest-asyncio==1.5.1
networkx==2.5
notebook==6.2.0
numpy==1.20.1
packaging==20.9
pandas==1.2.2
pandocfilters==1.4.3
parso==0.8.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==8.1.0
pluggy==0.13.1
prometheus-client==0.9.0
prompt-toolkit==3.0.16
ptyprocess==0.7.0
py==1.10.0
pycparser==2.20
pydantic==1.7.3
Pygments==2.8.0
pyparsing==2.4.7
pyproj==3.0.0.post1
pyrsistent==0.17.3
pyshp==2.1.3
pytest==6.2.2
python-dateutil==2.8.1
pytz==2021.1
PyWavelets==1.1.1
PyYAML==5.4.1
pyzmq==22.0.3
rasterio==1.2.0
requests==2.25.1
rio-cogeo==2.1.2
rioxarray==0.3.1
s3fs==0.4.2
s3transfer==0.3.4
scikit-image==0.18.1
scipy==1.6.1
seaborn==0.11.1
Send2Trash==1.5.0
Shapely==1.7.1
six==1.15.0
sniffio==1.2.0
snuggs==1.4.7
terminado==0.9.2
testpath==0.4.4
tifffile==2021.2.1
toml==0.10.2
toolz==0.11.1
tornado==6.1
traitlets==5.0.5
urllib3==1.26.3
wcwidth==0.2.5
webencodings==0.5.1
xarray==0.17.0
xlrd==2.0.1
snowman2 commented 3 years ago

I wonder how large your raster is? clipped.plot.imshow() could be the fix.

See: https://gis.stackexchange.com/questions/384577/open-and-plot-a-large-raster-with-python-xarray

robmarkcole commented 3 years ago

clipped.plot.imshow() does indeed work. It is only a small raster, y: 1537x: 2471

snowman2 commented 3 years ago

http://xarray.pydata.org/en/stable/generated/xarray.DataArray.plot.html#xarray.DataArray.plot

By default for 2D rasters, it calls xarray.plot.pcolormesh(). I think this is an xarray issue. I would raise this issue here if you would have thoughts or ideas for them for how this might be improved: https://github.com/pydata/xarray/