8000 Scatter plot with non-sequence ´c´ color should give a better Error message. · Issue #10365 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
Scatter plot with non-sequence ´c´ color should give a better Error message. #10365
Closed
@jason-neal

Description

@jason-neal

Bug report

Bug summary

Using a scatter plot with a single point at. But using a single number for the "c" keyword gave an slightly unhelpful error about a TypeError from mcolors.to_rgba_array. A cmap is used to map the number given but this is omitted here as the Error is still reproduced.

Code for reproduction

import maptlotlib.pyplot as plt
plt.scatter([0], [1], c=0.5)

Actual outcome

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-a8c544fd7de9> in <module>()
----> 1 plt.scatter([0], [1], c=0.5)

~/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, hold, data, **kwargs)
   3376                          vmin=vmin, vmax=vmax, alpha=alpha,
   3377                          linewidths=linewidths, verts=verts,
-> 3378                          edgecolors=edgecolors, data=data, **kwargs)
   3379     finally:
   3380         ax._hold = washold

~/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1715                     warnings.warn(msg % (label_namer, func.__name__),
   1716                                   RuntimeWarning, stacklevel=2)
-> 1717             return func(ax, *args, **kwargs)
   1718         pre_doc = inner.__doc__
   1719         if pre_doc is None:

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   3981             try:
   3982                 # must be acceptable as PathCollection facecolors
-> 3983                 colors = mcolors.to_rgba_array(c)
   3984             except ValueError:
   3985                 # c not acceptable as PathCollection facecolor

~/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba_array(c, alpha)
    229         pass
    230     # Convert one at a time.
--> 231     result = np.empty((len(c), 4), float)
    232     for i, cc in enumerate(c):
    233         result[i] = to_rgba(cc, alpha)

TypeError: object of type 'int' has no len()

Expected outcome

Have a useful message from an except TypeError: from the failing colors = mcolors.to_rgba_array(c) call in line 3983 much like the ValueError except, would probably do it.

Helpfully indicating that c must be the correct type (color, sequence, or sequence of color).

Note:
With x, y, and c all as single numbers the scatter plot call runs without any errors raised,
e.g. plt.scatter(0, 1, c=0.5)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0