You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

832 lines
38 KiB

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