Skip to content

Vague/misleading exception message in scatter() #12780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
steven-murray opened this issue Nov 8, 2018 · 4 comments
Closed

Vague/misleading exception message in scatter() #12780

steven-murray opened this issue Nov 8, 2018 · 4 comments

Comments

@steven-murray
Copy link

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

@anntzer
Copy link
Contributor

anntzer commented Nov 8, 2018

Thanks for the report! I think the exact same issue is already opened at #12735 (the last part), feel free to comment on that thread if there's anything else we're missing (or request a reopen if I missed anything).

@gecko17k
Copy link

I had this error,
The problem was that my Y was shape (341,1).
The solution was to reshape to a 1D array:
Y = Y.reshape(Y.shape[0])

@timhoffm
Copy link
Member

Which version of matplotlib?

@khollerb
Copy link

Useful. Had that also. In my case Y was shape (1,somenumber) so I reshaped to
Y = Y.reshape(Y.shape[1])
worked
matplotlib 3.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants