Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/matplotlib/sankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from matplotlib.path import Path
from matplotlib.patches import PathPatch
from matplotlib.transforms import Affine2D
from matplotlib import cbook, docstring
from matplotlib import docstring
from matplotlib import rcParams

_log = logging.getLogger(__name__)
Expand Down Expand Up @@ -453,12 +453,13 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
f"The shapes of 'flows' {np.shape(flows)} and 'orientations' "
f"{np.shape(orientations)} are incompatible"
) from None
if not cbook.is_scalar_or_string(labels) and len(labels) != n:
try:
labels = np.broadcast_to(labels, n)
except ValueError:
raise ValueError(
f"The lengths of 'flows' ({n}) and 'labels' ({len(labels)}) "
f"are incompatible")
else:
labels = [labels] * n
f"The shapes of 'flows' {np.shape(flows)} and 'labels' "
f"{np.shape(labels)} are incompatible"
) from None
if trunklength < 0:
raise ValueError(
"'trunklength' is negative, which is not allowed because it "
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/tests/test_sankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ def test_sankey():
# lets just create a sankey instance and check the code runs
sankey = Sankey()
sankey.add()


def test_label():
s = Sankey(flows=[0.25], labels=['First'], orientations=[-1])
assert s.diagrams[0].texts[0].get_text() == 'First\n0.25'