Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3166843

Browse files
committed
Improve stackplot example
1 parent 16ae357 commit 3166843

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

examples/lines_bars_and_markers/stackplot_demo.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,59 @@
11
"""
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+
===========================
115
"""
12-
import numpy as np
13-
import matplotlib.pyplot as plt
146

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.
1514

16-
# Fixing random state for reproducibility
17-
np.random.seed(19680801)
1815

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
2318

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+
}
2527

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)')
2734

28-
fig, ax = plt.subplots()
29-
ax.stackplot(x, y1, y2, y3, labels=labels)
30-
ax.legend(loc='upper left')
3135
plt.show()
3236

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.
3643

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)
3947

4048

4149
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*."""
4551
def bump(a):
46-
x = 1 / (.1 + np.random.random())
52+
amplitude = 1 / (.1 + np.random.random())
53+
x = np.linspace(0, 1, m)
4754
y = 2 * np.random.random() - .5
4855
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)
5257
a = np.zeros((m, n))
5358
for i in range(n):
5459
for j in range(5):

0 commit comments

Comments
 (0)