|
1 | 1 | """
|
2 |
| -============== |
3 |
| -Stackplot Demo |
4 |
| -============== |
5 |
| -
|
6 |
| -How to create stackplots with Matplotlib. |
7 |
| -
|
8 |
| -Stackplots are generated by plotting different datasets vertically on |
9 |
| -top of one another rather than overlapping with one another. Below we |
10 |
| -show some examples to accomplish this with Matplotlib. |
| 2 | +=========================== |
| 3 | +Stackplots and streamgraphs |
| 4 | +=========================== |
11 | 5 | """
|
12 |
| -import numpy as np |
13 |
| -import matplotlib.pyplot as plt |
14 | 6 |
|
| 7 | +############################################################################## |
| 8 | +# Stackplots |
| 9 | +# ---------- |
| 10 | +# |
| 11 | +# Stackplots draw multiple datasets as vertically stacked areas. This is |
| 12 | +# useful when the individual data values and addtionally their cumulative |
| 13 | +# value are of interest. |
15 | 14 |
|
16 |
| -# Fixing random state for reproducibility |
17 |
| -np.random.seed(19680801) |
18 | 15 |
|
19 |
| -x = [1, 2, 3, 4, 5] |
20 |
| -y1 = [1, 1, 2, 3, 5] |
21 |
| -y2 = [0, 4, 2, 6, 8] |
22 |
| -y3 = [1, 3, 5, 7, 9] |
| 16 | +import numpy as np |
| 17 | +import matplotlib.pyplot as plt |
23 | 18 |
|
24 |
| -y = np.vstack([y1, y2, y3]) |
| 19 | +year = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2018] |
| 20 | +population_by_contient = { |
| 21 | + 'africa': [228, 284, 365, 477, 631, 814, 1044, 1275], |
| 22 | + 'americas': [340, 425, 519, 619, 727, 840, 943, 1006], |
| 23 | + 'asia': [1394, 1686, 2120, 2625, 3202, 3714, 4169, 4560], |
| 24 | + 'europe': [220, 253, 276, 295, 310, 303, 294, 293], |
| 25 | + 'oceania': [12, 15, 19, 22, 26, 31, 36, 39], |
| 26 | +} |
25 | 27 |
|
26 |
| -labels = ["Fibonacci ", "Evens", "Odds"] |
| 28 | +plt.stackplot(year, population_by_contient.values(), |
| 29 | + labels=population_by_contient.keys()) |
| 30 | +plt.legend(loc='upper left') |
| 31 | +plt.title('World population') |
| 32 | +plt.xlabel('Year') |
| 33 | +plt.ylabel('Number of people (millions)') |
27 | 34 |
|
28 |
| -fig, ax = plt.subplots() |
29 |
| -ax.stackplot(x, y1, y2, y3, labels=labels) |
30 |
| -ax.legend(loc='upper left') |
31 | 35 | plt.show()
|
32 | 36 |
|
33 |
| -fig, ax = plt.subplots() |
34 |
| -ax.stackplot(x, y) |
35 |
| -plt.show() |
| 37 | +############################################################################## |
| 38 | +# Streamgraphs |
| 39 | +# ------------ |
| 40 | +# |
| 41 | +# Using the *baseline* parameter, you can turn an ordinary stacked area plot |
| 42 | +# with baseline 0 into a stream graph. |
36 | 43 |
|
37 |
| -############################################################################### |
38 |
| -# Here we show an example of making a streamgraph using stackplot |
| 44 | + |
| 45 | +# Fixing random state for reproducibility |
| 46 | +np.random.seed(19680801) |
39 | 47 |
|
40 | 48 |
|
41 | 49 | def layers(n, m):
|
42 |
| - """ |
43 |
| - Return *n* random Gaussian mixtures, each of length *m*. |
44 |
| - """ |
| 50 | + """Return *n* random Gaussian mixtures, each of length *m*.""" |
45 | 51 | def bump(a):
|
46 |
| - x = 1 / (.1 + np.random.random()) |
| 52 | + amplitude = 1 / (.1 + np.random.random()) |
| 53 | + x = np.linspace(0, 1, m) |
47 | 54 | y = 2 * np.random.random() - .5
|
48 | 55 | z = 10 / (.1 + np.random.random())
|
49 |
| - for i in range(m): |
50 |
| - w = (i / m - y) * z |
51 |
| - a[i] += x * np.exp(-w * w) |
| 56 | + a += amplitude * np.exp(-((x - y) * z)**2) |
52 | 57 | a = np.zeros((m, n))
|
53 | 58 | for i in range(n):
|
54 | 59 | for j in range(5):
|
|
0 commit comments