From 2cdcc03f32d6e8be3cbbd402b66b712efa8a5359 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 10 May 2021 10:23:08 +0200 Subject: [PATCH] Fix validation of Line2D color. We don't actually support color="auto" (we only support mfc="auto" and mec="auto", which mean "use the main color") -- with the sole exception of axisartist Ticks, which is a subclass that has special handling for "auto". This can easily be tested using `plt.plot([1, 2], c="auto")` which crashes. --- lib/matplotlib/lines.py | 3 +-- lib/mpl_toolkits/axisartist/axis_artist.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index a94a7aa4868e..229e81449d04 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -1027,8 +1027,7 @@ def set_color(self, color): ---------- color : color """ - if not cbook._str_equal(color, 'auto'): - mcolors._check_color_like(color=color) + mcolors._check_color_like(color=color) self._color = color self.stale = True diff --git a/lib/mpl_toolkits/axisartist/axis_artist.py b/lib/mpl_toolkits/axisartist/axis_artist.py index be055e9fa966..f1e323549372 100644 --- a/lib/mpl_toolkits/axisartist/axis_artist.py +++ b/lib/mpl_toolkits/axisartist/axis_artist.py @@ -75,10 +75,10 @@ import numpy as np -from matplotlib import _api, rcParams +from matplotlib import _api, cbook, rcParams import matplotlib.artist as martist +import matplotlib.colors as mcolors import matplotlib.text as mtext - from matplotlib.collections import LineCollection from matplotlib.lines import Line2D from matplotlib.patches import PathPatch @@ -134,6 +134,14 @@ def get_ref_artist(self): # docstring inherited return self._axis.majorTicks[0].tick1line + def set_color(self, color): + # docstring inherited + # Unlike the base Line2D.set_color, this also supports "auto". + if not cbook._str_equal(color, "auto"): + mcolors._check_color_like(color=color) + self._color = color + self.stale = True + def get_color(self): return self.get_attribute_from_ref_artist("color")