Skip to content

Commit 7b0414e

Browse files
committed
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...)
1 parent 70cef77 commit 7b0414e

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Passing a single string as *labels* to `Sankey.add`
2+
```````````````````````````````````````````````````
3+
4+
Previously, `Sankey.add` would only accept a single string as the *labels*
5+
argument if its length is equal to the number of flows, in which case it would
6+
use one character of the string for each flow.
7+
8+
The behavior has been changed to match the documented one: when a single string
9+
is passed, it is used to label all the flows.

lib/matplotlib/sankey.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from matplotlib.path import Path
1111
from matplotlib.patches import PathPatch
1212
from matplotlib.transforms import Affine2D
13-
from matplotlib import docstring
13+
from matplotlib import cbook, docstring
1414
from matplotlib import rcParams
1515

1616
_log = logging.getLogger(__name__)
@@ -449,11 +449,8 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
449449
"orientations and flows must have the same length.\n"
450450
"orientations has length %d, but flows has length %d."
451451
% (len(orientations), n))
452-
if labels != '' and getattr(labels, '__iter__', False):
453-
# np.iterable() isn't used because it would give True if labels is
454-
# a string
455-
if len(labels) != n:
456-
raise ValueError(
452+
if not cbook.is_scalar_or_string(labels) and len(labels) != n:
453+
raise ValueError(
457454
"If labels is a list, then labels and flows must have the "
458455
"same length.\nlabels has length %d, but flows has length %d."
459456
% (len(labels), n))

0 commit comments

Comments
 (0)