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.

120 lines
3.9 KiB

4 years ago
  1. """
  2. Stacked area plot for 1D arrays inspired by Douglas Y'barbo's stackoverflow
  3. answer:
  4. http://stackoverflow.com/questions/2225995/how-can-i-create-stacked-line-graph-with-matplotlib
  5. (http://stackoverflow.com/users/66549/doug)
  6. """
  7. import numpy as np
  8. __all__ = ['stackplot']
  9. def stackplot(axes, x, *args,
  10. labels=(), colors=None, baseline='zero',
  11. **kwargs):
  12. """
  13. Draw a stacked area plot.
  14. Parameters
  15. ----------
  16. x : 1d array of dimension N
  17. y : 2d array (dimension MxN), or sequence of 1d arrays (each dimension 1xN)
  18. The data is assumed to be unstacked. Each of the following
  19. calls is legal::
  20. stackplot(x, y) # where y is MxN
  21. stackplot(x, y1, y2, y3, y4) # where y1, y2, y3, y4, are all 1xNm
  22. baseline : {'zero', 'sym', 'wiggle', 'weighted_wiggle'}
  23. Method used to calculate the baseline:
  24. - ``'zero'``: Constant zero baseline, i.e. a simple stacked plot.
  25. - ``'sym'``: Symmetric around zero and is sometimes called
  26. 'ThemeRiver'.
  27. - ``'wiggle'``: Minimizes the sum of the squared slopes.
  28. - ``'weighted_wiggle'``: Does the same but weights to account for
  29. size of each layer. It is also called 'Streamgraph'-layout. More
  30. details can be found at http://leebyron.com/streamgraph/.
  31. labels : Length N sequence of strings
  32. Labels to assign to each data series.
  33. colors : Length N sequence of colors
  34. A list or tuple of colors. These will be cycled through and used to
  35. colour the stacked areas.
  36. **kwargs :
  37. All other keyword arguments are passed to `Axes.fill_between()`.
  38. Returns
  39. -------
  40. list : list of `.PolyCollection`
  41. A list of `.PolyCollection` instances, one for each element in the
  42. stacked area plot.
  43. """
  44. y = np.row_stack(args)
  45. labels = iter(labels)
  46. if colors is not None:
  47. axes.set_prop_cycle(color=colors)
  48. # Assume data passed has not been 'stacked', so stack it here.
  49. # We'll need a float buffer for the upcoming calculations.
  50. stack = np.cumsum(y, axis=0, dtype=np.promote_types(y.dtype, np.float32))
  51. if baseline == 'zero':
  52. first_line = 0.
  53. elif baseline == 'sym':
  54. first_line = -np.sum(y, 0) * 0.5
  55. stack += first_line[None, :]
  56. elif baseline == 'wiggle':
  57. m = y.shape[0]
  58. first_line = (y * (m - 0.5 - np.arange(m)[:, None])).sum(0)
  59. first_line /= -m
  60. stack += first_line
  61. elif baseline == 'weighted_wiggle':
  62. m, n = y.shape
  63. total = np.sum(y, 0)
  64. # multiply by 1/total (or zero) to avoid infinities in the division:
  65. inv_total = np.zeros_like(total)
  66. mask = total > 0
  67. inv_total[mask] = 1.0 / total[mask]
  68. increase = np.hstack((y[:, 0:1], np.diff(y)))
  69. below_size = total - stack
  70. below_size += 0.5 * y
  71. move_up = below_size * inv_total
  72. move_up[:, 0] = 0.5
  73. center = (move_up - 0.5) * increase
  74. center = np.cumsum(center.sum(0))
  75. first_line = center - 0.5 * total
  76. stack += first_line
  77. else:
  78. errstr = "Baseline method %s not recognised. " % baseline
  79. errstr += "Expected 'zero', 'sym', 'wiggle' or 'weighted_wiggle'"
  80. raise ValueError(errstr)
  81. # Color between x = 0 and the first array.
  82. color = axes._get_lines.get_next_color()
  83. coll = axes.fill_between(x, first_line, stack[0, :],
  84. facecolor=color, label=next(labels, None),
  85. **kwargs)
  86. coll.sticky_edges.y[:] = [0]
  87. r = [coll]
  88. # Color between array i-1 and array i
  89. for i in range(len(y) - 1):
  90. color = axes._get_lines.get_next_color()
  91. r.append(axes.fill_between(x, stack[i, :], stack[i + 1, :],
  92. facecolor=color, label=next(labels, None),
  93. **kwargs))
  94. return r