pydata / pydata/xarray

col_wrap arg to da.plot() can be a string?

Open
#5,614 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-error reporting topic-plotting
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened:
if you try to plot a data array with 3 dimensions but the third dim has length 1, then col_wrap accepts a dim name and plots facets with pcolormesh. according to the documentation col_wrap argument should be an int so I assume this is unintentional http://xarray.pydata.org/en/stable/generated/xarray.DataArray.plot.html

if dim 3 has length >1, then col_wrap still takes a str but uses hist()

What you expected to happen:
I expected an error if col_wrap is not an int. I also expected da.plot(col='3rd_dim') to give me one facet if the dim has length 1 - this (mis)behavior is described in #620

all a bit confusing :). I thought I understood plotting but then ran into bug #620 and started fumbling around. I am trying to plot a dataarray in a function but I don't know ahead of time how many values 3rd dim has. I also wanted to avoid limiting my function to pcolormesh() only. da.plot.pcolormesh(col='3rd_dim') works to plot the images regardless of dim length.

Minimal Complete Verifiable Example:

da_list = []
for i in np.arange(1,3):
    data = np.ones((64, 48), dtype = 'uint8') * (200-i*15)
    oneframe = xr.DataArray(
        data=data[..., None],
        dims=('x', 'y','measurement'),
        coords={'measurement':[f't{i*40}']}
    )

    da_list.append(oneframe)

twoframes = xr.concat(da_list, 'measurement')
oneframe.plot(col_wrap='measurement') # Oddly, facets with pcolormesh()
twoframes.plot(col_wrap='measurement') # Oddly, a single facet with hist()
oneframe.plot(col='measurement') # ValueError: IndexVariable objects must be 1-dimensional ala issue #620
twoframes.plot(col='measurement') # facets with pcolormesh()
oneframe.plot.pcolormesh(col='measurement') # facets with pcolormesh()
twoframes.plot.pcolormesh(col='measurement') #facets with pcolormesh()

Anything else we need to know?:
@nfahlgren noted a workaround using FacetGrid directly

from xarray.plot.facetgrid import FacetGrid
gd = FacetGrid(data=oneframe, col="measurement")
gd.map_dataarray(xr.plot.imshow, "x", "y")

is consistent with:

gd = FacetGrid(data=twoframes, col="measurement")
gd.map_dataarray(xr.plot.imshow, "x", "y")

Environment:
Windows 10, python 3.7

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 15:37:01) [MSC v.1916 64 bit (AMD64)]
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
libhdf5: None
libnetcdf: None

xarray: 0.17.0
pandas: 1.2.3
numpy: 1.20.1
scipy: 1.6.0
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.7.0
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2021.02.0
distributed: 2021.02.0
matplotlib: 3.3.4
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 49.6.0.post20210108
pip: 21.1.2
conda: 4.10.1
pytest: 6.2.2
IPython: 7.24.1
sphinx: None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal reproducible example against DataArray.plot, including pcolormesh, hist, and xarray.plot.facetgrid.FacetGrid. Trace how col and col_wrap are dispatched for one- and multi-value dimensions. Done means non-integer col_wrap is rejected and the requested single-value col behavior is consistent with the documented plotting API.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.