From 7b0414eaab4b9e50230b5b0eabff1593266fc169 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 29 Aug 2018 18:50:59 +0200 Subject: [PATCH] Make code match comment in sankey. This makes Sankey.add no longer reject a labels that's a *single* string, consistently with what the docstring states and what the comment just after implies. Previously, it would only accept a single string if its length is equal to the number of flows, in which case it would use one character of the string for each flow. Alternatively, we could also change the docstring and delete the comment, but I think this behavior is more intuitive? (Not that I ever use Sankey, though it looks neat...) --- doc/api/next_api_changes/2018-08-29-AL-sankey.rst | 9 +++++++++ lib/matplotlib/sankey.py | 9 +++------ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 doc/api/next_api_changes/2018-08-29-AL-sankey.rst diff --git a/doc/api/next_api_changes/2018-08-29-AL-sankey.rst b/doc/api/next_api_changes/2018-08-29-AL-sankey.rst new file mode 100644 index 000000000000..9386d5bde3ed --- /dev/null +++ b/doc/api/next_api_changes/2018-08-29-AL-sankey.rst @@ -0,0 +1,9 @@ +Passing a single string as *labels* to `Sankey.add` +``````````````````````````````````````````````````` + +Previously, `Sankey.add` would only accept a single string as the *labels* +argument if its length is equal to the number of flows, in which case it would +use one character of the string for each flow. + +The behavior has been changed to match the documented one: when a single string +is passed, it is used to label all the flows. diff --git a/lib/matplotlib/sankey.py b/lib/matplotlib/sankey.py index 77964a76882e..aa75a1d0bafa 100644 --- a/lib/matplotlib/sankey.py +++ b/lib/matplotlib/sankey.py @@ -10,7 +10,7 @@ from matplotlib.path import Path from matplotlib.patches import PathPatch from matplotlib.transforms import Affine2D -from matplotlib import docstring +from matplotlib import cbook, docstring from matplotlib import rcParams _log = logging.getLogger(__name__) @@ -449,11 +449,8 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='', "orientations and flows must have the same length.\n" "orientations has length %d, but flows has length %d." % (len(orientations), n)) - if labels != '' and getattr(labels, '__iter__', False): - # np.iterable() isn't used because it would give True if labels is - # a string - if len(labels) != n: - raise ValueError( + if not cbook.is_scalar_or_string(labels) and len(labels) != n: + raise ValueError( "If labels is a list, then labels and flows must have the " "same length.\nlabels has length %d, but flows has length %d." % (len(labels), n))