Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1aa5a83

Browse files
committed
Suppress chaining of cache lookup failure in color conversion.
Currently, something like `scatter(range(4), range(4), c=np.arange(4).reshape((2, 2)))` results in the following traceback: ``` Traceback (most recent call last): File "matplotlib/lib/matplotlib/colors.py", line 173, 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 "matplotlib/lib/matplotlib/axes/_axes.py", line 4289, in _parse_scatter_color_args colors = mcolors.to_rgba_array(c) File "matplotlib/lib/matplotlib/colors.py", line 284, in to_rgba_array result[i] = to_rgba(cc, alpha) File "matplotlib/lib/matplotlib/colors.py", line 175, in to_rgba rgba = _to_rgba_no_colorcycle(c, alpha) File "matplotlib/lib/matplotlib/colors.py", line 240, 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 "<string>", line 4, in <module> File "matplotlib/lib/matplotlib/pyplot.py", line 2826, in scatter None else {}), **kwargs) File "matplotlib/lib/matplotlib/__init__.py", line 1512, in inner return func(ax, *map(sanitize_sequence, args), **kwargs) File "matplotlib/lib/matplotlib/axes/_axes.py", line 4458, in scatter get_next_color_func=self._get_patches_for_fill.get_next_color) File "matplotlib/lib/matplotlib/axes/_axes.py", line 4302, in _parse_scatter_color_args .format(nc=n_elem, xs=xsize, ys=ysize) ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 4, 'y' with size 4. ``` At least the topmost failure, regarding cache lookup, is irrelevant to the end user, so suppress it, which this commit does. The middle traceback is mildly interesing, in case the user intended to pass in RGBA but mis-shaped it; we may want to fold the message into the scatter() error as well -- but that's not the object of this commit.
1 parent 6013532 commit 1aa5a83

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

lib/matplotlib/colors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def to_rgba(c, alpha=None):
172172
try:
173173
rgba = _colors_full_map.cache[c, alpha]
174174
except (KeyError, TypeError): # Not in cache, or unhashable.
175+
rgba = None
176+
if rgba is None: # Suppress exception chaining of cache lookup failure.
175177
rgba = _to_rgba_no_colorcycle(c, alpha)
176178
try:
177179
_colors_full_map.cache[c, alpha] = rgba

0 commit comments

Comments
 (0)