Skip to content

[Bug]: scatter issue plotting data from XArray DataSet with time on x-axis. #22023

@jdassink

Description

@jdassink

Bug summary

Scatter fails to plot data from XArray dataset. Seems to be related to the conversion of XArray time objects (datetime64[ns]) to Numpy date time. Seems like it works fine with xarray 0.16.2 but not version 0.20.1.

Code for reproduction

ds_td_s = xr.open_mfdataset(datafile)

plt.scatter(ds_td_s['time'], ds_td_s['sir'])
plt.show()

Actual outcome

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-7fd9da257a0f> in <module>
----> 1 plt.scatter(ds_td_s['time'], ds_td_s['snr'])
      2 plt.show()

~/opt/miniconda3/envs/py38/lib/python3.8/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, data, **kwargs)
   2888         verts=cbook.deprecation._deprecated_parameter,
   2889         edgecolors=None, *, plotnonfinite=False, data=None, **kwargs):
-> 2890     __ret = gca().scatter(
   2891         x, y, s=s, c=c, marker=marker, cmap=cmap, norm=norm,
   2892         vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,

~/opt/miniconda3/envs/py38/lib/python3.8/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1445     def inner(ax, *args, data=None, **kwargs):
   1446         if data is None:
-> 1447             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1448 
   1449         bound = new_sig.bind(ax, *args, **kwargs)

~/opt/miniconda3/envs/py38/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py in wrapper(*inner_args, **inner_kwargs)
    409                          else deprecation_addendum,
    410                 **kwargs)
--> 411         return func(*inner_args, **inner_kwargs)
    412 
    413     return wrapper

~/opt/miniconda3/envs/py38/lib/python3.8/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)
   4436         # np.ma.ravel yields an ndarray, not a masked array,
   4437         # unless its argument is a masked array.
-> 4438         x = np.ma.ravel(x)
   4439         y = np.ma.ravel(y)
   4440         if x.size != y.size:

~/opt/miniconda3/envs/py38/lib/python3.8/site-packages/numpy/ma/core.py in __call__(self, a, *args, **params)
   6768             a, args[0] = args[0], a
   6769 
-> 6770         marr = asanyarray(a)
   6771         method_name = self.__name__
   6772         method = getattr(type(marr), method_name, None)

~/opt/miniconda3/envs/py38/lib/python3.8/site-packages/numpy/ma/core.py in asanyarray(a, dtype)
   7996     if isinstance(a, MaskedArray) and (dtype is None or dtype == a.dtype):
   7997         return a
-> 7998     return masked_array(a, dtype=dtype, copy=False, keep_mask=True, subok=True)
   7999 
   8000 

~/opt/miniconda3/envs/py38/lib/python3.8/site-packages/numpy/ma/core.py in __new__(cls, data, mask, dtype, copy, subok, ndmin, fill_value, keep_mask, hard_mask, shrink, order, **options)
   2829         """
   2830         # Process data.
-> 2831         _data = np.array(data, dtype=dtype, copy=copy,
   2832                          order=order, subok=True, ndmin=ndmin)
   2833         _baseclass = getattr(data, '_baseclass', type(_data))

ValueError: Could not convert object to NumPy datetime

Expected outcome

Simple scatterplot of points

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.3.3

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions