-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Scatter plot with non-sequence ´c´ color should give a better Error message. #10365
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
Comments
@jason-neal thank you for the report. As far as I understand, the difference between case A # After this block, c_array will be None unless
# c is an array for mapping. The potential ambiguity
# with a sequence of 3 or 4 numbers is resolved in
# favor of mapping, not rgb or rgba.
if c_none or co is not None:
c_array = None
else: # <- from the not shown instructions above, c_none is False
try:
c_array = np.asanyarray(c, dtype=float)
if c_array.shape in xy_shape: # <- True for case *A* => everything will be fine
c = np.ma.ravel(c_array)
else: # <- in case *B* c_array.shape == (), while xy_shape == ((1,), (1,))
# Wrong size; it must not be intended for mapping.
c_array = None
except ValueError:
# Failed to make a floating-point array; c must be color specs.
c_array = None
if c_array is None: # <- True in case *B*
try:
# must be acceptable as PathCollection facecolors
colors = mcolors.to_rgba_array(c) # <- BOOM!
except ValueError:
# c not acceptable as PathCollection facecolor
msg = ("c of shape {0} not acceptable as a color sequence "
"for x with size {1}, y with size {2}")
raise ValueError(msg.format(c.shape, x.size, y.size))
else:
colors = None # use cmap, norm after collection is created |
Funnily, something like (Too bad I had a possible fix before noticing that behavior 😢) ---
lib/matplotlib/axes/_axes.py | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py
index df2ccd55e..2137f6805 100644
--- a/lib/matplotlib/axes/_axes.py
+++ b/lib/matplotlib/axes/_axes.py
@@ -4228,11 +4228,19 @@ linewidth=2, markersize=12)
try:
# must be acceptable as PathCollection facecolors
colors = mcolors.to_rgba_array(c)
- except ValueError:
+ except (ValueError, TypeError) as e:
# c not acceptable as PathCollection facecolor
- raise ValueError("c of shape {} not acceptable as a color "
- "sequence for x with size {}, y with size {}"
- .format(c.shape, x.size, y.size))
+ try:
+ msg = ("c of shape {} not acceptable as a color "
+ "sequence for x with size {}, y with size {}"
+ .format(c.shape, x.size, y.size))
+ except AttributeError:
+ # More verbose version that avoids `.shape`. Avoid `.size`
+ # as well to handle cases as `plt.scatter(0, 1, c=[0.5])`.
+ msg = ("c = {} is not of acceptable shape for color "
+ "mapping with x with shape {}, y with shape {}"
+ .format(c, *xy_shape))
+ raise type(e)(msg)
else:
colors = None # use cmap, norm after collection is created
--
2.14.3
|
Reading more carefully the docstring of
my understanding is that we are not supporting any of the following cases, among which some are actually not raising an error:
although for each case at least one of the parameter is not an “array-like”, contrary to what is asked in the docstring. PS: one can also note the inconsistent case between |
With the recent updates to scatter e.g. #11383 the examples given above now produce error messages.
The last example still works.
If this is supposed to work like this then this issue can probably be closed. |
leaving this comment here in hopes it may be useful to others who come across it: A solution that works for passing both arrays and scalars is:
I was updating code that had x and y formed by indexing arrays, but if you passed a length-1 index, it would return a scalar, not a length-1 array. This would result in the error
and I found the |
We definitely need more tests to make sure not to break the scatter API twice within two successive versions again. |
agreed. also, better documentation for that particular feature. Maybe a working example? This bug messed me up for such a long time when I upgraded and finding help for it was more difficult than it needed to be IMO.
… On Apr 2, 2019, at 2:10 PM, Elan Ernest ***@***.***> wrote:
We definitely need more tests to make sure not to break the scatter API twice within two successive versions again.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#10365 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/Amfwt_xY4w_1ekwx4EItnRmNiEfxvVItks5vc7kogaJpZM4R2eFV>.
|
Closing; mostly superseded by #12735. |
@jason-neal for the code below #Plot data, the color represents the #Make global variables that will be use often #make initial theta as an array of 1D #it gives |
It will be fixed 3.2 |
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
frommcolors.to_rgba_array
. Acmap
is used to map the number given but this is omitted here as the Error is still reproduced.Code for reproduction
Actual outcome
Expected outcome
Have a useful message from an
except TypeError:
from the failingcolors = mcolors.to_rgba_array(c)
call in line 3983 much like theValueError
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)
The text was updated successfully, but these errors were encountered: