Skip to content

Misleading error message in _parse_scatter_color_args #17243

Closed
@orrkrup

Description

@orrkrup

Bug report

Not really a bug, just an annoying error message.
In _axes.py, line 4283, the try clause catches errors in an unintended section of code and adds an error message that doesn't make sense to the traceback. For instance, if a ValueError is raised in the call to to_rgb_array(c), it will be caught and the error would be wrong.

Code for reproduction:

import matplotlib.pyplot as plt
import numpy as np

a = np.arange(10)
b = np.random.rand(10)
colors = np.random.rand(10, 3) * 2.0

plt.scatter(a, b, c=colors)

This results in the following traceback:

Traceback (most recent call last):
  File "/Users/user/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4284, in _parse_scatter_color_args
    colors = mcolors.to_rgba_array(c)
  File "/Users/user/lib/python3.6/site-packages/matplotlib/colors.py", line 279, in to_rgba_array
    raise ValueError("RGBA values should be within 0-1 range")
ValueError: RGBA values should be within 0-1 range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2841, in scatter
    None else {}), **kwargs)
  File "/Users/user/lib/python3.6/site-packages/matplotlib/__init__.py", line 1599, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/Users/user/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4453, in scatter
    get_next_color_func=self._get_patches_for_fill.get_next_color)
  File "/Users/user/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4297, in _parse_scatter_color_args
    .format(nc=n_elem, xs=xsize, ys=ysize)
ValueError: 'c' argument has 10 elements, which is not acceptable for use with 'x' with size 10, 'y' with size 10.

The first part of the traceback makes sense, but the last error message shouldn't be there, especially since it states something that is not true (number of elements is acceptable).

Matplotlib version

  • Operating system: MacOS 10.15.3
  • Matplotlib version: 3.1.2
  • Matplotlib backend: MacOSX
  • Python version: 3.6.8

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions