sankey.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. """
  2. Module for creating Sankey diagrams using Matplotlib.
  3. """
  4. import logging
  5. from types import SimpleNamespace
  6. import numpy as np
  7. import matplotlib as mpl
  8. from matplotlib.path import Path
  9. from matplotlib.patches import PathPatch
  10. from matplotlib.transforms import Affine2D
  11. from matplotlib import docstring
  12. _log = logging.getLogger(__name__)
  13. __author__ = "Kevin L. Davies"
  14. __credits__ = ["Yannick Copin"]
  15. __license__ = "BSD"
  16. __version__ = "2011/09/16"
  17. # Angles [deg/90]
  18. RIGHT = 0
  19. UP = 1
  20. # LEFT = 2
  21. DOWN = 3
  22. class Sankey:
  23. """
  24. Sankey diagram.
  25. Sankey diagrams are a specific type of flow diagram, in which
  26. the width of the arrows is shown proportionally to the flow
  27. quantity. They are typically used to visualize energy or
  28. material or cost transfers between processes.
  29. `Wikipedia (6/1/2011) <https://en.wikipedia.org/wiki/Sankey_diagram>`_
  30. """
  31. def __init__(self, ax=None, scale=1.0, unit='', format='%G', gap=0.25,
  32. radius=0.1, shoulder=0.03, offset=0.15, head_angle=100,
  33. margin=0.4, tolerance=1e-6, **kwargs):
  34. """
  35. Create a new Sankey instance.
  36. The optional arguments listed below are applied to all subdiagrams so
  37. that there is consistent alignment and formatting.
  38. In order to draw a complex Sankey diagram, create an instance of
  39. :class:`Sankey` by calling it without any kwargs::
  40. sankey = Sankey()
  41. Then add simple Sankey sub-diagrams::
  42. sankey.add() # 1
  43. sankey.add() # 2
  44. #...
  45. sankey.add() # n
  46. Finally, create the full diagram::
  47. sankey.finish()
  48. Or, instead, simply daisy-chain those calls::
  49. Sankey().add().add... .add().finish()
  50. Other Parameters
  51. ----------------
  52. ax : `~.axes.Axes`
  53. Axes onto which the data should be plotted. If *ax* isn't
  54. provided, new Axes will be created.
  55. scale : float
  56. Scaling factor for the flows. *scale* sizes the width of the paths
  57. in order to maintain proper layout. The same scale is applied to
  58. all subdiagrams. The value should be chosen such that the product
  59. of the scale and the sum of the inputs is approximately 1.0 (and
  60. the product of the scale and the sum of the outputs is
  61. approximately -1.0).
  62. unit : str
  63. The physical unit associated with the flow quantities. If *unit*
  64. is None, then none of the quantities are labeled.
  65. format : str
  66. A Python number formatting string to be used in labeling the flow
  67. as a quantity (i.e., a number times a unit, where the unit is
  68. given).
  69. gap : float
  70. Space between paths that break in/break away to/from the top or
  71. bottom.
  72. radius : float
  73. Inner radius of the vertical paths.
  74. shoulder : float
  75. Size of the shoulders of output arrows.
  76. offset : float
  77. Text offset (from the dip or tip of the arrow).
  78. head_angle : float
  79. Angle, in degrees, of the arrow heads (and negative of the angle of
  80. the tails).
  81. margin : float
  82. Minimum space between Sankey outlines and the edge of the plot
  83. area.
  84. tolerance : float
  85. Acceptable maximum of the magnitude of the sum of flows. The
  86. magnitude of the sum of connected flows cannot be greater than
  87. *tolerance*.
  88. **kwargs
  89. Any additional keyword arguments will be passed to :meth:`add`,
  90. which will create the first subdiagram.
  91. See Also
  92. --------
  93. Sankey.add
  94. Sankey.finish
  95. Examples
  96. --------
  97. .. plot:: gallery/specialty_plots/sankey_basics.py
  98. """
  99. # Check the arguments.
  100. if gap < 0:
  101. raise ValueError(
  102. "'gap' is negative, which is not allowed because it would "
  103. "cause the paths to overlap")
  104. if radius > gap:
  105. raise ValueError(
  106. "'radius' is greater than 'gap', which is not allowed because "
  107. "it would cause the paths to overlap")
  108. if head_angle < 0:
  109. raise ValueError(
  110. "'head_angle' is negative, which is not allowed because it "
  111. "would cause inputs to look like outputs and vice versa")
  112. if tolerance < 0:
  113. raise ValueError(
  114. "'tolerance' is negative, but it must be a magnitude")
  115. # Create axes if necessary.
  116. if ax is None:
  117. import matplotlib.pyplot as plt
  118. fig = plt.figure()
  119. ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[])
  120. self.diagrams = []
  121. # Store the inputs.
  122. self.ax = ax
  123. self.unit = unit
  124. self.format = format
  125. self.scale = scale
  126. self.gap = gap
  127. self.radius = radius
  128. self.shoulder = shoulder
  129. self.offset = offset
  130. self.margin = margin
  131. self.pitch = np.tan(np.pi * (1 - head_angle / 180.0) / 2.0)
  132. self.tolerance = tolerance
  133. # Initialize the vertices of tight box around the diagram(s).
  134. self.extent = np.array((np.inf, -np.inf, np.inf, -np.inf))
  135. # If there are any kwargs, create the first subdiagram.
  136. if len(kwargs):
  137. self.add(**kwargs)
  138. def _arc(self, quadrant=0, cw=True, radius=1, center=(0, 0)):
  139. """
  140. Return the codes and vertices for a rotated, scaled, and translated
  141. 90 degree arc.
  142. Other Parameters
  143. ----------------
  144. quadrant : {0, 1, 2, 3}, default: 0
  145. Uses 0-based indexing (0, 1, 2, or 3).
  146. cw : bool, default: True
  147. If True, the arc vertices are produced clockwise; counter-clockwise
  148. otherwise.
  149. radius : float, default: 1
  150. The radius of the arc.
  151. center : (float, float), default: (0, 0)
  152. (x, y) tuple of the arc's center.
  153. """
  154. # Note: It would be possible to use matplotlib's transforms to rotate,
  155. # scale, and translate the arc, but since the angles are discrete,
  156. # it's just as easy and maybe more efficient to do it here.
  157. ARC_CODES = [Path.LINETO,
  158. Path.CURVE4,
  159. Path.CURVE4,
  160. Path.CURVE4,
  161. Path.CURVE4,
  162. Path.CURVE4,
  163. Path.CURVE4]
  164. # Vertices of a cubic Bezier curve approximating a 90 deg arc
  165. # These can be determined by Path.arc(0, 90).
  166. ARC_VERTICES = np.array([[1.00000000e+00, 0.00000000e+00],
  167. [1.00000000e+00, 2.65114773e-01],
  168. [8.94571235e-01, 5.19642327e-01],
  169. [7.07106781e-01, 7.07106781e-01],
  170. [5.19642327e-01, 8.94571235e-01],
  171. [2.65114773e-01, 1.00000000e+00],
  172. # Insignificant
  173. # [6.12303177e-17, 1.00000000e+00]])
  174. [0.00000000e+00, 1.00000000e+00]])
  175. if quadrant == 0 or quadrant == 2:
  176. if cw:
  177. vertices = ARC_VERTICES
  178. else:
  179. vertices = ARC_VERTICES[:, ::-1] # Swap x and y.
  180. elif quadrant == 1 or quadrant == 3:
  181. # Negate x.
  182. if cw:
  183. # Swap x and y.
  184. vertices = np.column_stack((-ARC_VERTICES[:, 1],
  185. ARC_VERTICES[:, 0]))
  186. else:
  187. vertices = np.column_stack((-ARC_VERTICES[:, 0],
  188. ARC_VERTICES[:, 1]))
  189. if quadrant > 1:
  190. radius = -radius # Rotate 180 deg.
  191. return list(zip(ARC_CODES, radius * vertices +
  192. np.tile(center, (ARC_VERTICES.shape[0], 1))))
  193. def _add_input(self, path, angle, flow, length):
  194. """
  195. Add an input to a path and return its tip and label locations.
  196. """
  197. if angle is None:
  198. return [0, 0], [0, 0]
  199. else:
  200. x, y = path[-1][1] # Use the last point as a reference.
  201. dipdepth = (flow / 2) * self.pitch
  202. if angle == RIGHT:
  203. x -= length
  204. dip = [x + dipdepth, y + flow / 2.0]
  205. path.extend([(Path.LINETO, [x, y]),
  206. (Path.LINETO, dip),
  207. (Path.LINETO, [x, y + flow]),
  208. (Path.LINETO, [x + self.gap, y + flow])])
  209. label_location = [dip[0] - self.offset, dip[1]]
  210. else: # Vertical
  211. x -= self.gap
  212. if angle == UP:
  213. sign = 1
  214. else:
  215. sign = -1
  216. dip = [x - flow / 2, y - sign * (length - dipdepth)]
  217. if angle == DOWN:
  218. quadrant = 2
  219. else:
  220. quadrant = 1
  221. # Inner arc isn't needed if inner radius is zero
  222. if self.radius:
  223. path.extend(self._arc(quadrant=quadrant,
  224. cw=angle == UP,
  225. radius=self.radius,
  226. center=(x + self.radius,
  227. y - sign * self.radius)))
  228. else:
  229. path.append((Path.LINETO, [x, y]))
  230. path.extend([(Path.LINETO, [x, y - sign * length]),
  231. (Path.LINETO, dip),
  232. (Path.LINETO, [x - flow, y - sign * length])])
  233. path.extend(self._arc(quadrant=quadrant,
  234. cw=angle == DOWN,
  235. radius=flow + self.radius,
  236. center=(x + self.radius,
  237. y - sign * self.radius)))
  238. path.append((Path.LINETO, [x - flow, y + sign * flow]))
  239. label_location = [dip[0], dip[1] - sign * self.offset]
  240. return dip, label_location
  241. def _add_output(self, path, angle, flow, length):
  242. """
  243. Append an output to a path and return its tip and label locations.
  244. .. note:: *flow* is negative for an output.
  245. """
  246. if angle is None:
  247. return [0, 0], [0, 0]
  248. else:
  249. x, y = path[-1][1] # Use the last point as a reference.
  250. tipheight = (self.shoulder - flow / 2) * self.pitch
  251. if angle == RIGHT:
  252. x += length
  253. tip = [x + tipheight, y + flow / 2.0]
  254. path.extend([(Path.LINETO, [x, y]),
  255. (Path.LINETO, [x, y + self.shoulder]),
  256. (Path.LINETO, tip),
  257. (Path.LINETO, [x, y - self.shoulder + flow]),
  258. (Path.LINETO, [x, y + flow]),
  259. (Path.LINETO, [x - self.gap, y + flow])])
  260. label_location = [tip[0] + self.offset, tip[1]]
  261. else: # Vertical
  262. x += self.gap
  263. if angle == UP:
  264. sign = 1
  265. else:
  266. sign = -1
  267. tip = [x - flow / 2.0, y + sign * (length + tipheight)]
  268. if angle == UP:
  269. quadrant = 3
  270. else:
  271. quadrant = 0
  272. # Inner arc isn't needed if inner radius is zero
  273. if self.radius:
  274. path.extend(self._arc(quadrant=quadrant,
  275. cw=angle == UP,
  276. radius=self.radius,
  277. center=(x - self.radius,
  278. y + sign * self.radius)))
  279. else:
  280. path.append((Path.LINETO, [x, y]))
  281. path.extend([(Path.LINETO, [x, y + sign * length]),
  282. (Path.LINETO, [x - self.shoulder,
  283. y + sign * length]),
  284. (Path.LINETO, tip),
  285. (Path.LINETO, [x + self.shoulder - flow,
  286. y + sign * length]),
  287. (Path.LINETO, [x - flow, y + sign * length])])
  288. path.extend(self._arc(quadrant=quadrant,
  289. cw=angle == DOWN,
  290. radius=self.radius - flow,
  291. center=(x - self.radius,
  292. y + sign * self.radius)))
  293. path.append((Path.LINETO, [x - flow, y + sign * flow]))
  294. label_location = [tip[0], tip[1] + sign * self.offset]
  295. return tip, label_location
  296. def _revert(self, path, first_action=Path.LINETO):
  297. """
  298. A path is not simply reversible by path[::-1] since the code
  299. specifies an action to take from the **previous** point.
  300. """
  301. reverse_path = []
  302. next_code = first_action
  303. for code, position in path[::-1]:
  304. reverse_path.append((next_code, position))
  305. next_code = code
  306. return reverse_path
  307. # This might be more efficient, but it fails because 'tuple' object
  308. # doesn't support item assignment:
  309. # path[1] = path[1][-1:0:-1]
  310. # path[1][0] = first_action
  311. # path[2] = path[2][::-1]
  312. # return path
  313. @docstring.dedent_interpd
  314. def add(self, patchlabel='', flows=None, orientations=None, labels='',
  315. trunklength=1.0, pathlengths=0.25, prior=None, connect=(0, 0),
  316. rotation=0, **kwargs):
  317. """
  318. Add a simple Sankey diagram with flows at the same hierarchical level.
  319. Parameters
  320. ----------
  321. patchlabel : str
  322. Label to be placed at the center of the diagram.
  323. Note that *label* (not *patchlabel*) can be passed as keyword
  324. argument to create an entry in the legend.
  325. flows : list of float
  326. Array of flow values. By convention, inputs are positive and
  327. outputs are negative.
  328. Flows are placed along the top of the diagram from the inside out
  329. in order of their index within *flows*. They are placed along the
  330. sides of the diagram from the top down and along the bottom from
  331. the outside in.
  332. If the sum of the inputs and outputs is
  333. nonzero, the discrepancy will appear as a cubic Bezier curve along
  334. the top and bottom edges of the trunk.
  335. orientations : list of {-1, 0, 1}
  336. List of orientations of the flows (or a single orientation to be
  337. used for all flows). Valid values are 0 (inputs from
  338. the left, outputs to the right), 1 (from and to the top) or -1
  339. (from and to the bottom).
  340. labels : list of (str or None)
  341. List of labels for the flows (or a single label to be used for all
  342. flows). Each label may be *None* (no label), or a labeling string.
  343. If an entry is a (possibly empty) string, then the quantity for the
  344. corresponding flow will be shown below the string. However, if
  345. the *unit* of the main diagram is None, then quantities are never
  346. shown, regardless of the value of this argument.
  347. trunklength : float
  348. Length between the bases of the input and output groups (in
  349. data-space units).
  350. pathlengths : list of float
  351. List of lengths of the vertical arrows before break-in or after
  352. break-away. If a single value is given, then it will be applied to
  353. the first (inside) paths on the top and bottom, and the length of
  354. all other arrows will be justified accordingly. The *pathlengths*
  355. are not applied to the horizontal inputs and outputs.
  356. prior : int
  357. Index of the prior diagram to which this diagram should be
  358. connected.
  359. connect : (int, int)
  360. A (prior, this) tuple indexing the flow of the prior diagram and
  361. the flow of this diagram which should be connected. If this is the
  362. first diagram or *prior* is *None*, *connect* will be ignored.
  363. rotation : float
  364. Angle of rotation of the diagram in degrees. The interpretation of
  365. the *orientations* argument will be rotated accordingly (e.g., if
  366. *rotation* == 90, an *orientations* entry of 1 means to/from the
  367. left). *rotation* is ignored if this diagram is connected to an
  368. existing one (using *prior* and *connect*).
  369. Returns
  370. -------
  371. Sankey
  372. The current `.Sankey` instance.
  373. Other Parameters
  374. ----------------
  375. **kwargs
  376. Additional keyword arguments set `matplotlib.patches.PathPatch`
  377. properties, listed below. For example, one may want to use
  378. ``fill=False`` or ``label="A legend entry"``.
  379. %(Patch)s
  380. See Also
  381. --------
  382. Sankey.finish
  383. """
  384. # Check and preprocess the arguments.
  385. if flows is None:
  386. flows = np.array([1.0, -1.0])
  387. else:
  388. flows = np.array(flows)
  389. n = flows.shape[0] # Number of flows
  390. if rotation is None:
  391. rotation = 0
  392. else:
  393. # In the code below, angles are expressed in deg/90.
  394. rotation /= 90.0
  395. if orientations is None:
  396. orientations = 0
  397. try:
  398. orientations = np.broadcast_to(orientations, n)
  399. except ValueError:
  400. raise ValueError(
  401. f"The shapes of 'flows' {np.shape(flows)} and 'orientations' "
  402. f"{np.shape(orientations)} are incompatible"
  403. ) from None
  404. try:
  405. labels = np.broadcast_to(labels, n)
  406. except ValueError:
  407. raise ValueError(
  408. f"The shapes of 'flows' {np.shape(flows)} and 'labels' "
  409. f"{np.shape(labels)} are incompatible"
  410. ) from None
  411. if trunklength < 0:
  412. raise ValueError(
  413. "'trunklength' is negative, which is not allowed because it "
  414. "would cause poor layout")
  415. if abs(np.sum(flows)) > self.tolerance:
  416. _log.info("The sum of the flows is nonzero (%f; patchlabel=%r); "
  417. "is the system not at steady state?",
  418. np.sum(flows), patchlabel)
  419. scaled_flows = self.scale * flows
  420. gain = sum(max(flow, 0) for flow in scaled_flows)
  421. loss = sum(min(flow, 0) for flow in scaled_flows)
  422. if prior is not None:
  423. if prior < 0:
  424. raise ValueError("The index of the prior diagram is negative")
  425. if min(connect) < 0:
  426. raise ValueError(
  427. "At least one of the connection indices is negative")
  428. if prior >= len(self.diagrams):
  429. raise ValueError(
  430. f"The index of the prior diagram is {prior}, but there "
  431. f"are only {len(self.diagrams)} other diagrams")
  432. if connect[0] >= len(self.diagrams[prior].flows):
  433. raise ValueError(
  434. "The connection index to the source diagram is {}, but "
  435. "that diagram has only {} flows".format(
  436. connect[0], len(self.diagrams[prior].flows)))
  437. if connect[1] >= n:
  438. raise ValueError(
  439. f"The connection index to this diagram is {connect[1]}, "
  440. f"but this diagram has only {n} flows")
  441. if self.diagrams[prior].angles[connect[0]] is None:
  442. raise ValueError(
  443. f"The connection cannot be made, which may occur if the "
  444. f"magnitude of flow {connect[0]} of diagram {prior} is "
  445. f"less than the specified tolerance")
  446. flow_error = (self.diagrams[prior].flows[connect[0]] +
  447. flows[connect[1]])
  448. if abs(flow_error) >= self.tolerance:
  449. raise ValueError(
  450. f"The scaled sum of the connected flows is {flow_error}, "
  451. f"which is not within the tolerance ({self.tolerance})")
  452. # Determine if the flows are inputs.
  453. are_inputs = [None] * n
  454. for i, flow in enumerate(flows):
  455. if flow >= self.tolerance:
  456. are_inputs[i] = True
  457. elif flow <= -self.tolerance:
  458. are_inputs[i] = False
  459. else:
  460. _log.info(
  461. "The magnitude of flow %d (%f) is below the tolerance "
  462. "(%f).\nIt will not be shown, and it cannot be used in a "
  463. "connection.", i, flow, self.tolerance)
  464. # Determine the angles of the arrows (before rotation).
  465. angles = [None] * n
  466. for i, (orient, is_input) in enumerate(zip(orientations, are_inputs)):
  467. if orient == 1:
  468. if is_input:
  469. angles[i] = DOWN
  470. elif not is_input:
  471. # Be specific since is_input can be None.
  472. angles[i] = UP
  473. elif orient == 0:
  474. if is_input is not None:
  475. angles[i] = RIGHT
  476. else:
  477. if orient != -1:
  478. raise ValueError(
  479. f"The value of orientations[{i}] is {orient}, "
  480. f"but it must be -1, 0, or 1")
  481. if is_input:
  482. angles[i] = UP
  483. elif not is_input:
  484. angles[i] = DOWN
  485. # Justify the lengths of the paths.
  486. if np.iterable(pathlengths):
  487. if len(pathlengths) != n:
  488. raise ValueError(
  489. f"The lengths of 'flows' ({n}) and 'pathlengths' "
  490. f"({len(pathlengths)}) are incompatible")
  491. else: # Make pathlengths into a list.
  492. urlength = pathlengths
  493. ullength = pathlengths
  494. lrlength = pathlengths
  495. lllength = pathlengths
  496. d = dict(RIGHT=pathlengths)
  497. pathlengths = [d.get(angle, 0) for angle in angles]
  498. # Determine the lengths of the top-side arrows
  499. # from the middle outwards.
  500. for i, (angle, is_input, flow) in enumerate(zip(angles, are_inputs,
  501. scaled_flows)):
  502. if angle == DOWN and is_input:
  503. pathlengths[i] = ullength
  504. ullength += flow
  505. elif angle == UP and not is_input:
  506. pathlengths[i] = urlength
  507. urlength -= flow # Flow is negative for outputs.
  508. # Determine the lengths of the bottom-side arrows
  509. # from the middle outwards.
  510. for i, (angle, is_input, flow) in enumerate(reversed(list(zip(
  511. angles, are_inputs, scaled_flows)))):
  512. if angle == UP and is_input:
  513. pathlengths[n - i - 1] = lllength
  514. lllength += flow
  515. elif angle == DOWN and not is_input:
  516. pathlengths[n - i - 1] = lrlength
  517. lrlength -= flow
  518. # Determine the lengths of the left-side arrows
  519. # from the bottom upwards.
  520. has_left_input = False
  521. for i, (angle, is_input, spec) in enumerate(reversed(list(zip(
  522. angles, are_inputs, zip(scaled_flows, pathlengths))))):
  523. if angle == RIGHT:
  524. if is_input:
  525. if has_left_input:
  526. pathlengths[n - i - 1] = 0
  527. else:
  528. has_left_input = True
  529. # Determine the lengths of the right-side arrows
  530. # from the top downwards.
  531. has_right_output = False
  532. for i, (angle, is_input, spec) in enumerate(zip(
  533. angles, are_inputs, list(zip(scaled_flows, pathlengths)))):
  534. if angle == RIGHT:
  535. if not is_input:
  536. if has_right_output:
  537. pathlengths[i] = 0
  538. else:
  539. has_right_output = True
  540. # Begin the subpaths, and smooth the transition if the sum of the flows
  541. # is nonzero.
  542. urpath = [(Path.MOVETO, [(self.gap - trunklength / 2.0), # Upper right
  543. gain / 2.0]),
  544. (Path.LINETO, [(self.gap - trunklength / 2.0) / 2.0,
  545. gain / 2.0]),
  546. (Path.CURVE4, [(self.gap - trunklength / 2.0) / 8.0,
  547. gain / 2.0]),
  548. (Path.CURVE4, [(trunklength / 2.0 - self.gap) / 8.0,
  549. -loss / 2.0]),
  550. (Path.LINETO, [(trunklength / 2.0 - self.gap) / 2.0,
  551. -loss / 2.0]),
  552. (Path.LINETO, [(trunklength / 2.0 - self.gap),
  553. -loss / 2.0])]
  554. llpath = [(Path.LINETO, [(trunklength / 2.0 - self.gap), # Lower left
  555. loss / 2.0]),
  556. (Path.LINETO, [(trunklength / 2.0 - self.gap) / 2.0,
  557. loss / 2.0]),
  558. (Path.CURVE4, [(trunklength / 2.0 - self.gap) / 8.0,
  559. loss / 2.0]),
  560. (Path.CURVE4, [(self.gap - trunklength / 2.0) / 8.0,
  561. -gain / 2.0]),
  562. (Path.LINETO, [(self.gap - trunklength / 2.0) / 2.0,
  563. -gain / 2.0]),
  564. (Path.LINETO, [(self.gap - trunklength / 2.0),
  565. -gain / 2.0])]
  566. lrpath = [(Path.LINETO, [(trunklength / 2.0 - self.gap), # Lower right
  567. loss / 2.0])]
  568. ulpath = [(Path.LINETO, [self.gap - trunklength / 2.0, # Upper left
  569. gain / 2.0])]
  570. # Add the subpaths and assign the locations of the tips and labels.
  571. tips = np.zeros((n, 2))
  572. label_locations = np.zeros((n, 2))
  573. # Add the top-side inputs and outputs from the middle outwards.
  574. for i, (angle, is_input, spec) in enumerate(zip(
  575. angles, are_inputs, list(zip(scaled_flows, pathlengths)))):
  576. if angle == DOWN and is_input:
  577. tips[i, :], label_locations[i, :] = self._add_input(
  578. ulpath, angle, *spec)
  579. elif angle == UP and not is_input:
  580. tips[i, :], label_locations[i, :] = self._add_output(
  581. urpath, angle, *spec)
  582. # Add the bottom-side inputs and outputs from the middle outwards.
  583. for i, (angle, is_input, spec) in enumerate(reversed(list(zip(
  584. angles, are_inputs, list(zip(scaled_flows, pathlengths)))))):
  585. if angle == UP and is_input:
  586. tip, label_location = self._add_input(llpath, angle, *spec)
  587. tips[n - i - 1, :] = tip
  588. label_locations[n - i - 1, :] = label_location
  589. elif angle == DOWN and not is_input:
  590. tip, label_location = self._add_output(lrpath, angle, *spec)
  591. tips[n - i - 1, :] = tip
  592. label_locations[n - i - 1, :] = label_location
  593. # Add the left-side inputs from the bottom upwards.
  594. has_left_input = False
  595. for i, (angle, is_input, spec) in enumerate(reversed(list(zip(
  596. angles, are_inputs, list(zip(scaled_flows, pathlengths)))))):
  597. if angle == RIGHT and is_input:
  598. if not has_left_input:
  599. # Make sure the lower path extends
  600. # at least as far as the upper one.
  601. if llpath[-1][1][0] > ulpath[-1][1][0]:
  602. llpath.append((Path.LINETO, [ulpath[-1][1][0],
  603. llpath[-1][1][1]]))
  604. has_left_input = True
  605. tip, label_location = self._add_input(llpath, angle, *spec)
  606. tips[n - i - 1, :] = tip
  607. label_locations[n - i - 1, :] = label_location
  608. # Add the right-side outputs from the top downwards.
  609. has_right_output = False
  610. for i, (angle, is_input, spec) in enumerate(zip(
  611. angles, are_inputs, list(zip(scaled_flows, pathlengths)))):
  612. if angle == RIGHT and not is_input:
  613. if not has_right_output:
  614. # Make sure the upper path extends
  615. # at least as far as the lower one.
  616. if urpath[-1][1][0] < lrpath[-1][1][0]:
  617. urpath.append((Path.LINETO, [lrpath[-1][1][0],
  618. urpath[-1][1][1]]))
  619. has_right_output = True
  620. tips[i, :], label_locations[i, :] = self._add_output(
  621. urpath, angle, *spec)
  622. # Trim any hanging vertices.
  623. if not has_left_input:
  624. ulpath.pop()
  625. llpath.pop()
  626. if not has_right_output:
  627. lrpath.pop()
  628. urpath.pop()
  629. # Concatenate the subpaths in the correct order (clockwise from top).
  630. path = (urpath + self._revert(lrpath) + llpath + self._revert(ulpath) +
  631. [(Path.CLOSEPOLY, urpath[0][1])])
  632. # Create a patch with the Sankey outline.
  633. codes, vertices = zip(*path)
  634. vertices = np.array(vertices)
  635. def _get_angle(a, r):
  636. if a is None:
  637. return None
  638. else:
  639. return a + r
  640. if prior is None:
  641. if rotation != 0: # By default, none of this is needed.
  642. angles = [_get_angle(angle, rotation) for angle in angles]
  643. rotate = Affine2D().rotate_deg(rotation * 90).transform_affine
  644. tips = rotate(tips)
  645. label_locations = rotate(label_locations)
  646. vertices = rotate(vertices)
  647. text = self.ax.text(0, 0, s=patchlabel, ha='center', va='center')
  648. else:
  649. rotation = (self.diagrams[prior].angles[connect[0]] -
  650. angles[connect[1]])
  651. angles = [_get_angle(angle, rotation) for angle in angles]
  652. rotate = Affine2D().rotate_deg(rotation * 90).transform_affine
  653. tips = rotate(tips)
  654. offset = self.diagrams[prior].tips[connect[0]] - tips[connect[1]]
  655. translate = Affine2D().translate(*offset).transform_affine
  656. tips = translate(tips)
  657. label_locations = translate(rotate(label_locations))
  658. vertices = translate(rotate(vertices))
  659. kwds = dict(s=patchlabel, ha='center', va='center')
  660. text = self.ax.text(*offset, **kwds)
  661. if mpl.rcParams['_internal.classic_mode']:
  662. fc = kwargs.pop('fc', kwargs.pop('facecolor', '#bfd1d4'))
  663. lw = kwargs.pop('lw', kwargs.pop('linewidth', 0.5))
  664. else:
  665. fc = kwargs.pop('fc', kwargs.pop('facecolor', None))
  666. lw = kwargs.pop('lw', kwargs.pop('linewidth', None))
  667. if fc is None:
  668. fc = next(self.ax._get_patches_for_fill.prop_cycler)['color']
  669. patch = PathPatch(Path(vertices, codes), fc=fc, lw=lw, **kwargs)
  670. self.ax.add_patch(patch)
  671. # Add the path labels.
  672. texts = []
  673. for number, angle, label, location in zip(flows, angles, labels,
  674. label_locations):
  675. if label is None or angle is None:
  676. label = ''
  677. elif self.unit is not None:
  678. quantity = self.format % abs(number) + self.unit
  679. if label != '':
  680. label += "\n"
  681. label += quantity
  682. texts.append(self.ax.text(x=location[0], y=location[1],
  683. s=label,
  684. ha='center', va='center'))
  685. # Text objects are placed even they are empty (as long as the magnitude
  686. # of the corresponding flow is larger than the tolerance) in case the
  687. # user wants to provide labels later.
  688. # Expand the size of the diagram if necessary.
  689. self.extent = (min(np.min(vertices[:, 0]),
  690. np.min(label_locations[:, 0]),
  691. self.extent[0]),
  692. max(np.max(vertices[:, 0]),
  693. np.max(label_locations[:, 0]),
  694. self.extent[1]),
  695. min(np.min(vertices[:, 1]),
  696. np.min(label_locations[:, 1]),
  697. self.extent[2]),
  698. max(np.max(vertices[:, 1]),
  699. np.max(label_locations[:, 1]),
  700. self.extent[3]))
  701. # Include both vertices _and_ label locations in the extents; there are
  702. # where either could determine the margins (e.g., arrow shoulders).
  703. # Add this diagram as a subdiagram.
  704. self.diagrams.append(
  705. SimpleNamespace(patch=patch, flows=flows, angles=angles, tips=tips,
  706. text=text, texts=texts))
  707. # Allow a daisy-chained call structure (see docstring for the class).
  708. return self
  709. def finish(self):
  710. """
  711. Adjust the axes and return a list of information about the Sankey
  712. subdiagram(s).
  713. Return value is a list of subdiagrams represented with the following
  714. fields:
  715. =============== ===================================================
  716. Field Description
  717. =============== ===================================================
  718. *patch* Sankey outline (an instance of
  719. :class:`~matplotlib.patches.PathPatch`)
  720. *flows* values of the flows (positive for input, negative
  721. for output)
  722. *angles* list of angles of the arrows [deg/90]
  723. For example, if the diagram has not been rotated,
  724. an input to the top side will have an angle of 3
  725. (DOWN), and an output from the top side will have
  726. an angle of 1 (UP). If a flow has been skipped
  727. (because its magnitude is less than *tolerance*),
  728. then its angle will be *None*.
  729. *tips* array in which each row is an [x, y] pair
  730. indicating the positions of the tips (or "dips") of
  731. the flow paths
  732. If the magnitude of a flow is less the *tolerance*
  733. for the instance of :class:`Sankey`, the flow is
  734. skipped and its tip will be at the center of the
  735. diagram.
  736. *text* :class:`~matplotlib.text.Text` instance for the
  737. label of the diagram
  738. *texts* list of :class:`~matplotlib.text.Text` instances
  739. for the labels of flows
  740. =============== ===================================================
  741. See Also
  742. --------
  743. Sankey.add
  744. """
  745. self.ax.axis([self.extent[0] - self.margin,
  746. self.extent[1] + self.margin,
  747. self.extent[2] - self.margin,
  748. self.extent[3] + self.margin])
  749. self.ax.set_aspect('equal', adjustable='datalim')
  750. return self.diagrams