From d350e5687c32f9e5ce8383613233ad3e6a96ec7b Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 28 Feb 2024 10:55:19 +0100 Subject: [PATCH] Simplify color/marker disambiguation logic in _process_plot_format. There's a bit of code in the parsing of plot() format shorthands to disambiguate the "1" marker and the "1.0" grayscale color string. It's actually easier to just explicitly check for the bad strings. This also makes it clearer that we *could* have supported "0" as unambiguous color string (white) as there's no "0" marker (there's a 0 (int) marker, which is not the same...), but let's not bother with changing any semantics. --- lib/matplotlib/axes/_base.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 0617fc7681bd..ed25a32d15bb 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -143,24 +143,16 @@ def _process_plot_format(fmt, *, ambiguous_fmt_datakey=False): marker = None color = None - # Is fmt just a colorspec? - try: - color = mcolors.to_rgba(fmt) - - # We need to differentiate grayscale '1.0' from tri_down marker '1' + # First check whether fmt is just a colorspec, but specifically exclude the + # grayscale string "1" (not "1.0"), which is interpreted as the tri_down + # marker "1". The grayscale string "0" could be unambiguously understood + # as a color (black) but also excluded for consistency. + if fmt not in ["0", "1"]: try: - fmtint = str(int(fmt)) + color = mcolors.to_rgba(fmt) + return linestyle, marker, color except ValueError: - return linestyle, marker, color # Yes - else: - if fmt != fmtint: - # user definitely doesn't want tri_down marker - return linestyle, marker, color # Yes - else: - # ignore converted color - color = None - except ValueError: - pass # No, not just a color. + pass errfmt = ("{!r} is neither a data key nor a valid format string ({})" if ambiguous_fmt_datakey else