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

proplot having issues with xarray objects #22

Closed
bradyrx opened this issue Jun 27, 2019 · 4 comments
Closed

proplot having issues with xarray objects #22

bradyrx opened this issue Jun 27, 2019 · 4 comments
Labels

Comments

@bradyrx
Copy link
Collaborator

bradyrx commented Jun 27, 2019

Currently, when plotting values from an xarray.DataArray, proplot throws an error. Note that this didn't used to be an issue.

The following works (note A.values has to be called, but A.time.values does not. So this is only an issue with the actual data being plotted and not coordinates)

import numpy as np
import xarray as xr
A = np.random.rand(120,)
A = xr.DataArray(A, dims='time')
A['time'] = np.arange('1990-01', '2000-01', dtype='datetime64[M]')
f, ax = plot.subplots(width='12cm', aspect=4)
ax.plot(A.time, A.values)

This does not work:

import numpy as np
import xarray as xr
A = np.random.rand(120,)
A = xr.DataArray(A, dims='time')
A['time'] = np.arange('1990-01', '2000-01', dtype='datetime64[M]')
f, ax = plot.subplots(width='12cm', aspect=4)
ax.plot(A.time, A)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-37-ae88108929a8> in <module>
      5 A['time'] = np.arange('1990-01', '2000-01', dtype='datetime64[M]')
      6 f, ax = plot.subplots(width='12cm', aspect=4)
----> 7 ax.plot(A.time, A)

~/miniconda3/envs/python3/lib/python3.7/site-packages/proplot/subplots.py in iterator(*args, **kwargs)
    129                 ret = []
    130                 for func in attrs:
--> 131                     ret.append(func(*args, **kwargs))
    132                 if len(ret)==1:
    133                     return ret[0]

~/miniconda3/envs/python3/lib/python3.7/site-packages/proplot/wrappers.py in wrapper(*args, **kwargs)
   2555         @functools.wraps(func)
   2556         def wrapper(*args, **kwargs):
-> 2557             return driver(self, func, *args, **kwargs)
   2558         return wrapper
   2559     return decorator

~/miniconda3/envs/python3/lib/python3.7/site-packages/proplot/wrappers.py in _parse_1d(self, func, *args, **kwargs)
    312     if kw:
    313         self.format(**kw)
--> 314     return func(x, *yss, *args, **kwargs)
    315 
    316 def _parse_2d(self, func, *args, order='C', **kwargs):

~/miniconda3/envs/python3/lib/python3.7/site-packages/proplot/wrappers.py in wrapper(*args, **kwargs)
   2555         @functools.wraps(func)
   2556         def wrapper(*args, **kwargs):
-> 2557             return driver(self, func, *args, **kwargs)
   2558         return wrapper
   2559     return decorator

~/miniconda3/envs/python3/lib/python3.7/site-packages/proplot/wrappers.py in plot_wrapper(self, func, cmap, values, *args, **kwargs)
    455         raise ValueError(f'Expected 1-3 plot args, got {len(args)}.')
    456     if cmap is None:
--> 457         lines = func(*args, **kwargs)
    458     else:
    459         lines = self.cmapline(*args, cmap=cmap, values=values, **kwargs)

~/miniconda3/envs/python3/lib/python3.7/site-packages/proplot/wrappers.py in wrapper(*args, **kwargs)
   2555         @functools.wraps(func)
   2556         def wrapper(*args, **kwargs):
-> 2557             return driver(self, func, *args, **kwargs)
   2558         return wrapper
   2559     return decorator

~/miniconda3/envs/python3/lib/python3.7/site-packages/proplot/wrappers.py in cycle_wrapper(self, func, cycle, cycle_kw, markers, linestyles, label, labels, values, legend, legend_kw, colorbar, colorbar_kw, *args, **kwargs)
   1517                 pass
   1518             elif isinstance(y, DataArray):
-> 1519                 label = y.coords[y.dims[1]].values[i]
   1520                 label_cl = _auto_label(y.coords[y.dims[1]]) # coordinate label
   1521             elif isinstance(y, DataFrame):

IndexError: tuple index out of range
@lukelbd
Copy link
Collaborator

lukelbd commented Jun 27, 2019

This is related to the "auto-formatting" feature; for some reason I forgot to account for 1d DataArrays. This patch (9a3d14e) seems to fix it.

@lukelbd lukelbd closed this as completed Jun 27, 2019
@bradyrx
Copy link
Collaborator Author

bradyrx commented Jun 27, 2019

That fixed that issue, thanks @lukelbd .

Although a new one I noticed as well (still related to xarray objects if you can reopen). proplot seems to take xarray variable names/dimension names along for the ride and auto-labels axes and titles.

A = np.random.rand(100,)
A = xr.DataArray(A, dims='time')
A.name = 'SST'
f, ax = plot.subplots(width='12cm', aspect=4)
ax.plot(A)

Screen Shot 2019-06-27 at 3 23 47 PM

Adding ax.format(xlabel='', title='') only removes the xlabel.

Screen Shot 2019-06-27 at 3 23 54 PM

@lukelbd
Copy link
Collaborator

lukelbd commented Jun 27, 2019

Yep that's right, it's intended behavior in case the DataArray does not have e.g. long_name or units attributes. Do you think that is inappropriate/annoying?

You can disable autoformatting behavior by (1) obviously passing A.values instead of A to the plotting command or (2) using plot.subplots(..., autoformat=False).

@lukelbd
Copy link
Collaborator

lukelbd commented Jun 28, 2019

...and now I understand the issue with not being able to reset the title specifically. This commit (c7cd02b) repairs this.

@lukelbd lukelbd added the bug label Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants