Skip to content

Two docs examples not working with latest xarray #4598

Closed
@LiamConnors

Description

@LiamConnors

When working on updating docs depedencies in #4596, I found the following two examples that don't work on the latest xarray.

import plotly.express as px
import xarray as xr
# Load xarray from dataset included in the xarray tutorial
airtemps = xr.tutorial.open_dataset('air_temperature').air.sel(lon=250.0)
fig = px.imshow(airtemps.T, color_continuous_scale='RdBu_r', origin='lower')
fig.show()
import plotly.express as px
import xarray as xr
# Load xarray from dataset included in the xarray tutorial
airtemps = xr.tutorial.open_dataset('air_temperature').air.sel(lon=250.0)
fig = px.imshow(airtemps.T, color_continuous_scale='RdBu_r', origin='lower')
fig.show()

Show the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line 5
      3 # Load xarray from dataset included in the xarray tutorial
      4 airtemps = xr.tutorial.open_dataset('air_temperature').air.sel(lon=250.0)
----> 5 fig = px.imshow(airtemps.T, color_continuous_scale='RdBu_r', origin='lower')
      6 fig.show()

File ~/miniconda3/envs/plotly-dev-2/lib/python3.10/site-packages/plotly/express/_imshow.py:300, in imshow(img, zmin, zmax, origin, labels, x, y, animation_frame, facet_col, facet_col_wrap, facet_col_spacing, facet_row_spacing, color_continuous_scale, color_continuous_midpoint, range_color, title, template, width, height, aspect, contrast_rescaling, binary_string, binary_backend, binary_compression_level, binary_format, text_auto)
    298         labels["facet_col"] = facet_label
    299     if labels.get("color", None) is None:
--> 300         labels["color"] = xarray.plot.utils.label_from_attrs(img)
    301         labels["color"] = labels["color"].replace("\n", "<br>")
    302 else:

File ~/miniconda3/envs/plotly-dev-2/lib/python3.10/site-packages/xarray/plot/utils.py:529, in label_from_attrs(da, extra)
    526 else:
    527     name = ""
--> 529 units = _get_units_from_attrs(da)
    531 # Treat `name` differently if it's a latex sequence
    532 if name.startswith("$") and (name.count("$") % 2 == 0):

File ~/miniconda3/envs/plotly-dev-2/lib/python3.10/site-packages/xarray/plot/utils.py:504, in _get_units_from_attrs(da)
    502 pint_array_type = DuckArrayModule("pint").type
    503 units = " [{}]"
--> 504 if isinstance(da.data, pint_array_type):
    505     return units.format(str(da.data.units))
    506 if "units" in da.attrs:

File ~/miniconda3/envs/plotly-dev-2/lib/python3.10/site-packages/xarray/core/common.py:285, in AttrAccessMixin.__getattr__(self, name)
    283         with suppress(KeyError):
    284             return source[name]
--> 285 raise AttributeError(
    286     f"{type(self).__name__!r} object has no attribute {name!r}"
    287 )

AttributeError: 'DataArray' object has no attribute 'data'


requirements:

alabaster                     0.7.16
anyio                         4.3.0
appnope                       0.1.4
argon2-cffi                   23.1.0
argon2-cffi-bindings          21.2.0
asttokens                     2.4.1
attrs                         23.2.0
Babel                         2.14.0
biopython                     1.83
bleach                        6.1.0
certifi                       2024.2.2
cffi                          1.16.0
charset-normalizer            3.3.2
click                         8.1.7
click-plugins                 1.1.1
cligj                         0.7.2
cloudpickle                   3.0.0
colorcet                      3.1.0
colorlover                    0.3.0
colour                        0.1.5
comm                          0.2.2
commonmark                    0.9.1
cufflinks                     0.17.3
dash                          2.16.1
dash-bio                      1.0.2
dash-core-components          2.0.0
dash-html-components          2.0.0
dash-table                    5.0.0
dask                          2024.4.2
datashader                    0.16.1
debugpy                       1.8.1
decorator                     5.1.1
defusedxml                    0.7.1
docutils                      0.16
entrypoints                   0.4
exceptiongroup                1.2.1
executing                     2.0.1
fastjsonschema                2.19.1
fiona                         1.9.6
Flask                         2.2.5
fsspec                        2024.3.1
geopandas                     0.8.1
GEOparse                      2.0.4
idna                          3.7
igraph                        0.11.4
imageio                       2.34.1
imagesize                     1.4.1
importlib_metadata            7.1.0
ipykernel                     6.29.4
ipython                       8.24.0
ipython-genutils              0.2.0
ipywidgets                    7.7.2
itsdangerous                  2.2.0
jedi                          0.19.1
Jinja2                        3.0.3
joblib                        1.4.0
jsonschema                    4.22.0
jsonschema-specifications     2023.12.1
jupyter                       1.0.0
jupyter-client                6.1.12
jupyter-console               6.4.2
jupyter_core                  5.7.2
jupyter-server                1.15.6
jupyterlab-widgets            1.1.7
jupytext                      1.1.1
kaleido                       0.2.1
lazy_loader                   0.4
llvmlite                      0.42.0
locket                        1.0.0
MarkupSafe                    2.1.5
matplotlib-inline             0.1.7
mistune                       0.8.4
mock                          5.1.0
multipledispatch              1.0.0
nbclassic                     1.0.0
nbconvert                     5.6.1
nbformat                      5.10.4
nest-asyncio                  1.6.0
networkx                      3.3
notebook                      6.5.6
notebook_shim                 0.2.4
numba                         0.59.1
numpy                         1.26.4
orjson                        3.10.2
packaging                     24.0
pandas                        2.2.2
pandocfilters                 1.5.1
param                         2.1.0
ParmEd                        4.2.2
parso                         0.8.4
partd                         1.4.1
pathlib                       1.0.1
patsy                         0.5.6
periodictable                 1.7.0
pexpect                       4.9.0
pillow                        10.3.0
pip                           24.0
platformdirs                  4.2.1
plotly                        5.22.0
plotly-geo                    1.0.0
polars                        0.20.23
pooch                         1.8.1
prometheus_client             0.20.0
prompt-toolkit                3.0.43
psutil                        5.9.8
ptyprocess                    0.7.0
pure-eval                     0.2.2
pyarrow                       16.0.0
pycparser                     2.22
pyct                          0.5.0
Pygments                      2.17.2
pynndescent                   0.5.12
pyparsing                     3.1.2
pyproj                        3.6.1
pyshp                         2.1.2
python-dateutil               2.9.0.post0
python-frontmatter            1.1.0
pytz                          2024.1
PyYAML                        6.0.1
pyzmq                         24.0.1
qtconsole                     5.5.1
QtPy                          2.4.1
recommonmark                  0.7.1
referencing                   0.35.0
requests                      2.31.0
retrying                      1.3.4
rpds-py                       0.18.0
scikit-image                  0.23.2
scikit-learn                  1.4.2
scipy                         1.13.0
Send2Trash                    1.8.3
setuptools                    69.5.1
shapely                       2.0.4
six                           1.16.0
sniffio                       1.3.1
snowballstemmer               2.2.0
Sphinx                        3.5.4
sphinx-bootstrap-theme        0.8.1
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        2.0.0
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.5
squarify                      0.4.3
stack-data                    0.6.3
statsmodels                   0.14.2
tenacity                      8.2.3
terminado                     0.18.1
testfixtures                  8.2.0
testpath                      0.6.0
texttable                     1.7.0
threadpoolctl                 3.5.0
tifffile                      2024.4.24
toolz                         0.12.1
tornado                       6.4
tqdm                          4.66.2
traitlets                     5.14.3
typing_extensions             4.11.0
tzdata                        2024.1
umap-learn                    0.5.1
urllib3                       2.2.1
wcwidth                       0.2.13
webencodings                  0.5.1
websocket-client              1.8.0
Werkzeug                      3.0.2
wget                          3.2
wheel                         0.43.0
widgetsnbextension            3.6.6
xarray                        2024.3.0
zipp                          3.18.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions