proplot icon indicating copy to clipboard operation
proplot copied to clipboard

Error when plotting with a `cftime.datetime` axis

Open spencerkclark opened this issue 3 years ago • 0 comments

Description

I've seen some discussion of making plots with cftime.datetime axes through the nc-time-axis package in this repo. Currently it appears that this functionality is broken.

Steps to reproduce

import cftime
import nc_time_axis
fig, ax = proplot.subplots(ncols=1, nrows=1)
ax.plot([cftime.DatetimeGregorian(2000, 1, d) for d in range(1, 6)], np.arange(5))

Expected behavior:

No error.

Actual behavior:

This error occurs:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-47-21c01342316d> in <module>
      3 fig, ax = proplot.subplots(ncols=1, nrows=1)
      4 
----> 5 ax.plot([cftime.DatetimeGregorian(2000, 1, d) for d in range(1, 6)], np.arange(5))

~/miniconda3/envs/fv3net/lib/python3.7/site-packages/proplot/internals/process.py in _redirect_or_standardize(self, *args, **kwargs)
    282 
    283                 # Call main function
--> 284                 return func(self, *args, **kwargs)  # call unbound method
    285 
    286         return _redirect_or_standardize

~/miniconda3/envs/fv3net/lib/python3.7/site-packages/proplot/axes/plot.py in plot(self, *args, **kwargs)
   2877         """
   2878         kwargs = _parse_vert(default_vert=True, **kwargs)
-> 2879         return self._apply_plot(*args, **kwargs)
   2880 
   2881     @process._preprocess_args('y', 'x', allow_extra=True)

~/miniconda3/envs/fv3net/lib/python3.7/site-packages/proplot/axes/plot.py in _apply_plot(self, vert, *pairs, **kwargs)
   2851 
   2852         # Add sticky edges
-> 2853         self._add_sticky_edges(objs, 'x' if vert else 'y', xsides, only=mlines.Line2D)
   2854         self._update_guide(objs, **guide_kw)
   2855         return cbook.silent_list('Line2D', objs)  # always return list

~/miniconda3/envs/fv3net/lib/python3.7/site-packages/proplot/axes/plot.py in _add_sticky_edges(self, objs, axis, only, *args)
   1511             if not sides.size:
   1512                 continue
-> 1513             min_, max_ = process._safe_range(sides)
   1514             if min_ is None or max_ is None:
   1515                 continue

~/miniconda3/envs/fv3net/lib/python3.7/site-packages/proplot/internals/process.py in _safe_range(data, lo, hi)
    488     """
    489     _load_objects()
--> 490     data, units = _to_masked_array(data)
    491     data = data.compressed()  # remove all invalid values
    492     min_ = max_ = None

~/miniconda3/envs/fv3net/lib/python3.7/site-packages/proplot/internals/process.py in _to_masked_array(data, copy)
    141     if ndarray is not Quantity and isinstance(data, Quantity):
    142         data, units = data.magnitude, data.units
--> 143     data = ma.masked_invalid(data, copy=copy)
    144     if np.issubdtype(data.dtype, np.integer):
    145         data = data.astype(np.float)

~/miniconda3/envs/fv3net/lib/python3.7/site-packages/numpy/ma/core.py in masked_invalid(a, copy)
   2369         cls = type(a)
   2370     else:
-> 2371         condition = ~(np.isfinite(a))
   2372         cls = MaskedArray
   2373     result = a.view(cls)

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Equivalent steps in matplotlib

Please try to make sure this bug is related to a proplot-specific feature. If you're not sure, try to replicate it with the native matplotlib API. Matplotlib bugs belong on the matplotlib github page.

import cftime
import matplotlib.pyplot as plt
import nc_time_axis
fig, ax = plt.subplots(ncols=1, nrows=1)
ax.plot([cftime.DatetimeGregorian(2000, 1, d) for d in range(1, 6)], np.arange(5))

Proplot version

Paste the results of import matplotlib; print(matplotlib.__version__); import proplot; print(proplot.version)here.

3.2.2
0.9.5

spencerkclark avatar Feb 09 '22 17:02 spencerkclark