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

Skip to content

Commit 4e967f3

Browse files
authored
Merge pull request #13218 from anntzer/sankey-labels
Fix checking of 'labels' argument to Sankey.add.
2 parents feac1ec + ee15f8a commit 4e967f3

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lib/matplotlib/sankey.py

Lines changed: 7 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 cbook, docstring
13+
from matplotlib import docstring
1414
from matplotlib import rcParams
1515

1616
_log = logging.getLogger(__name__)
@@ -453,12 +453,13 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
453453
f"The shapes of 'flows' {np.shape(flows)} and 'orientations' "
454454
f"{np.shape(orientations)} are incompatible"
455455
) from None
456-
if not cbook.is_scalar_or_string(labels) and len(labels) != n:
456+
try:
457+
labels = np.broadcast_to(labels, n)
458+
except ValueError:
457459
raise ValueError(
458-
f"The lengths of 'flows' ({n}) and 'labels' ({len(labels)}) "
459-
f"are incompatible")
460-
else:
461-
labels = [labels] * n
460+
f"The shapes of 'flows' {np.shape(flows)} and 'labels' "
461+
f"{np.shape(labels)} are incompatible"
462+
) from None
462463
if trunklength < 0:
463464
raise ValueError(
464465
"'trunklength' is negative, which is not allowed because it "

lib/matplotlib/tests/test_sankey.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ def test_sankey():
55
# lets just create a sankey instance and check the code runs
66
sankey = Sankey()
77
sankey.add()
8+
9+
10+
def test_label():
11+
s = Sankey(flows=[0.25], labels=['First'], orientations=[-1])
12+
assert s.diagrams[0].texts[0].get_text() == 'First\n0.25'

0 commit comments

Comments
 (0)