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

Skip to content

Error when color value is None #4192

@kyleam

Description

@kyleam

Commit d58a84a changed validate_colors so that value in rcParams is None rather than "None". However, in some cases, it seems that a string is still expected downstream.

For example,

import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['patch.edgecolor'] = None

plt.plot(list(range(5)))

results in

Traceback (most recent call last):
  File "[...]/matplotlib/lib/matplotlib/colors.py", line 322, in to_rgb
    'cannot convert argument to rgb sequence')
ValueError: cannot convert argument to rgb sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "[...]/matplotlib/lib/matplotlib/colors.py", line 370, in to_rgba
    r, g, b = self.to_rgb(arg)
  File "[...]/matplotlib/lib/matplotlib/colors.py", line 328, in to_rgb
    'to_rgb: Invalid rgb arg "%s"\n%s' % (str(arg), exc))
ValueError: to_rgb: Invalid rgb arg "None"
cannot convert argument to rgb sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./scratch.py", line 9, in <module>
    plt.plot(list(range(5)))
  File "[...]/matplotlib/lib/matplotlib/pyplot.py", line 3092, in plot
    ax = gca()
  File "[...]/matplotlib/lib/matplotlib/pyplot.py", line 827, in gca
    ax =  gcf().gca(**kwargs)
  File "[...]/matplotlib/lib/matplotlib/figure.py", line 1288, in gca
    return self.add_subplot(1, 1, 1, **kwargs)
  File "[...]/matplotlib/lib/matplotlib/figure.py", line 973, in add_subplot
    a = subplot_class_factory(projection_class)(self, *args, **kwargs)
  File "[...]/matplotlib/lib/matplotlib/axes/_subplots.py", line 73, in __init__
    self._axes_class.__init__(self, fig, self.figbox, **kwargs)
  File "[...]/matplotlib/lib/matplotlib/axes/_base.py", line 422, in __init__
    self.spines = self._gen_axes_spines()
  File "[...]/matplotlib/lib/matplotlib/axes/_base.py", line 812, in _gen_axes_spines
    'left': mspines.Spine.linear_spine(self, 'left'),
  File "[...]/matplotlib/lib/matplotlib/spines.py", line 454, in linear_spine
    result = cls(axes, spine_type, path, **kwargs)
  File "[...]/matplotlib/lib/matplotlib/spines.py", line 54, in __init__
    super(Spine, self).__init__(**kwargs)
  File "[...]/matplotlib/lib/matplotlib/patches.py", line 111, in __init__
    self.set_edgecolor(edgecolor)
  File "[...]/matplotlib/lib/matplotlib/patches.py", line 274, in set_edgecolor
    self._edgecolor = colors.colorConverter.to_rgba(color, self._alpha)
  File "[...]/matplotlib/lib/matplotlib/colors.py", line 376, in to_rgba
    'to_rgba: Invalid rgba arg "%s"\n%s' % (str(arg), exc))
ValueError: to_rgba: Invalid rgba arg "None"
to_rgb: Invalid rgb arg "None"
cannot convert argument to rgb sequence

This particular error can be fixed with

diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py
index 772da0f..810a528 100644
--- a/lib/matplotlib/colors.py
+++ b/lib/matplotlib/colors.py
@@ -342,7 +342,7 @@ def to_rgba(self, arg, alpha=None):
         *alpha* will replace the original *A*.
         """
         try:
-            if arg.lower() == 'none':
+            if arg is None or arg.lower() == 'none':
                 return (0.0, 0.0, 0.0, 0.0)
         except AttributeError:
             pass

A few other places also seem to expect a string.

lib/matplotlib/collections.py:572:            if c.lower() == 'none':
lib/matplotlib/collections.py:611:            if c.lower() == 'none':
lib/matplotlib/colors.py:393:            if nc == 0 or c.lower() == 'none':
lib/matplotlib/lines.py:725:            if is_string_like(edgecolor) and edgecolor.lower() == 'none':
lib/matplotlib/lines.py:1155:        if is_string_like(facecolor) and facecolor.lower() == 'none':
lib/matplotlib/lines.py:1163:        if is_string_like(facecolor) and facecolor.lower() == 'none':

Should these cases be updated to support None? I'd be happy to submit a PR, but I'm not clear on what the intended behavior is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions