Skip to content

Vague/misleading exception message in scatter() #12780

Closed
@steven-murray

Description

@steven-murray

Bug report

Bug summary

When using scatter, with the c= argument, I accidentally used an (n,2) array, instead of what I intended (an n-length vector). As expected, this did raise an error, however the error is misleading, and doesn't help much with debugging.

Code for reproduction

import matplotlib.pyplot  as plt
import numpy as np

x = np.linspace(0,1,10)
col = np.random.random(size=(10,2))
plt.scatter(x,x,c=col) # woops! should have been plt.scatter(x,x, c=col[:,0]).

Actual outcome

Traceback (most recent call last):
  File "/home/steven/miniconda3/envs/py21cmmc_fg/lib/python3.7/site-packages/matplotlib/colors.py", line 158, in to_rgba
    rgba = _colors_full_map.cache[c, alpha]
TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/steven/miniconda3/envs/py21cmmc_fg/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 4210, in scatter
    colors = mcolors.to_rgba_array(c)
  File "/home/steven/miniconda3/envs/py21cmmc_fg/lib/python3.7/site-packages/matplotlib/colors.py", line 259, in to_rgba_array
    result[i] = to_rgba(cc, alpha)
  File "/home/steven/miniconda3/envs/py21cmmc_fg/lib/python3.7/site-packages/matplotlib/colors.py", line 160, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "/home/steven/miniconda3/envs/py21cmmc_fg/lib/python3.7/site-packages/matplotlib/colors.py", line 215, in _to_rgba_no_colorcycle
    raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "instrumental_tests.py", line 218, in <module>
    test_imaging_single_source()
  File "instrumental_tests.py", line 156, in test_imaging_single_source
    test_imaging(SingleSource())
  File "instrumental_tests.py", line 91, in test_imaging
    c=np.real(ctx.get("visibilities")))
  File "/home/steven/miniconda3/envs/py21cmmc_fg/lib/python3.7/site-packages/matplotlib/__init__.py", line 1785, in inner
    return func(ax, *args, **kwargs)
  File "/home/steven/miniconda3/envs/py21cmmc_fg/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 4223, in scatter
    .format(nc=n_elem, xs=x.size, ys=y.size)
ValueError: 'c' argument has 10 elements, which is not acceptable for use with 'x' with size 10, 'y' with size 10.

Expected outcome

Expected exception would have been something like either ValueError: 'c' argument has 20 elements, which is not acceptable for use with 'x' with size 10, 'y' with size 10., or even better: ValueError: 'c' argument has shape (10,2), which is not acceptable for use with 'x' with size 10, 'y' with size 10.

Matplotlib version
v3.0.0

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