8000 Scatter: empty np.arrays with non-numeric dtypes cause TypeError · Issue #12118 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
Scatter: empty np.arrays with non-numeric dtypes cause TypeError #12118
Closed
@mattfabina

Description

@mattfabina

Bug report

Scatter: empty np.arrays with non-numeric dtypes cause error; empty np.arrays with numeric dtypes do not

This behavior is inconsistent, and causes issues when generically handling arrays of varying types that may or may not be empty. Empty arrays of numeric types pass through without any points being drawn, as desired; empty arrays of non-numeric types cause a TypeError to be thrown in numpy. I believe the offending code is in this function:

https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/units.py#L140

I'm not familiar with how this function works in the greater context of the project, but it seems to me that it should return a proper converter even for empty np.arrays. My current work-around is to detect empty arrays of non-numeric types and cast them to a numeric type before passing to scatter.

Code for reproduction

# is okay
plt.scatter(x=np.array([], dtype=float), y=np.array([], dtype=float))
# is okay
plt.scatter(x=np.array([], dtype=int), y=np.array([], dtype=float))
# raises TypeError
plt.scatter(x=np.array([], dtype='datetime64[ns]'), y=np.array([], dtype=float))

Actual outcome

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-e515f5294fc6> in <module>()
----> 1 plt.scatter(x=np.array([], dtype='datetime64[ns]'), y=np.array([], dtype=float))

[redacted]/python3.6/site-packages/matplotlib-2.2.3-py3.6-linux-x86_64.egg/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, hold, data, **kwargs)
   3473                          vmin=vmin, vmax=vmax, alpha=alpha,
   3474                          linewidths=linewidths, verts=verts,
-> 3475                          edgecolors=edgecolors, data=data, **kwargs)
   3476     finally:
   3477         ax._hold = washold

[redacted]/python3.6/site-packages/matplotlib-2.2.3-py3.6-linux-x86_64.egg/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1865                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1866                         RuntimeWarning, stacklevel=2)
-> 1867             return func(ax, *args, **kwargs)
   1868 
   1869         inner.__doc__ = _add_data_doc(inner.__doc__,

[redacted]/python3.6/site-packages/matplotlib-2.2.3-py3.6-linux-x86_64.egg/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4323             linewidths = rcParams['lines.linewidth']
   4324 
-> 4325         offsets = np.column_stack([x, y])
   4326 
   4327         collection = mcoll.PathCollection(

[redacted]/python3.6/site-packages/numpy/lib/shape_base.py in column_stack(tup)
    367             arr = array(arr, copy=False, subok=True, ndmin=2).T
    368         arrays.append(arr)
--> 369     return _nx.concatenate(arrays, 1)
    370 
    371 def dstack(tup):

TypeError: invalid type promotion

Expected outcome

The call to scatter should pass through without drawing any points, but also without raising any errors

Matplotlib version

  • Operating system: linux-x86_64
  • Matplotlib version: 2.2.3
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg (probably not relevant?)
  • Python version: 3.6
  • Jupyter version (if applicable): not installed
  • Other libraries: numpy 1.14.5

matplotlib, numpy installed via pip in virtualenv

Thank you!

Many thanks in advance! Excellent work has been done on this project, and I can't overstate my appreciation! Also kudos for the fantastic bug reporting form.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0