I was wondering how to create a 100 % stacked area chart in matplotlib. At the matplotlib page I couldn't find an example for it.
我想知道如何在matplotlib中创建一个100%的堆叠区域图。在matplotlib页面,我找不到一个例子。
Somebody here can show me how to achieve that?
有人能告诉我怎么做吗?
1 个解决方案
#1
17
A simple way to achieve this is to make sure that for every x-value, the y-values sum to 100.
实现这一点的一个简单方法是确保对于每一个x值,y值之和为100。
I assume that you have the y-values organized in an array as in the example below, i.e.
我假设你有一个数组中的y值,如下面的例子所示。
y = np.array([[17, 19, 5, 16, 22, 20, 9, 31, 39, 8],
[46, 18, 37, 27, 29, 6, 5, 23, 22, 5],
[15, 46, 33, 36, 11, 13, 39, 17, 49, 17]])
To make sure the column totals are 100, you have to divide the y
array by its column sums, and then multiply by 100. This makes the y-values span from 0 to 100, making the "unit" of the y-axis percent. If you instead want the values of the y-axis to span the interval from 0 to 1, don't multiply by 100.
为了确保列总数为100,必须将y数组除以它的列和,然后再乘以100。这使得y值跨度从0到100,使y轴的“单位”为百分比。如果你想让y轴的值跨度从0到1,不要乘以100。
Even if you don't have the y-values organized in one array as above, the principle is the same; the corresponding elements in each array consisting of y-values (e.g. y1
, y2
etc.) should sum to 100 (or 1).
即使没有如上所述在一个数组中组织y值,原理也是一样的;每个数组中包含y值(如y1, y2等)的对应元素之和应该是100(或1)。
The below code is a modified version of the example @LogicalKnight linked to in his comment.
下面的代码是经过修改的@LogicalKnight示例的一个版本。
import numpy as np
from matplotlib import pyplot as plt
fnx = lambda : np.random.randint(5, 50, 10)
y = np.row_stack((fnx(), fnx(), fnx()))
x = np.arange(10)
# Make new array consisting of fractions of column-totals,
# using .astype(float) to avoid integer division
percent = y / y.sum(axis=0).astype(float) * 100
fig = plt.figure()
ax = fig.add_subplot(111)
ax.stackplot(x, percent)
ax.set_title('100 % stacked area chart')
ax.set_ylabel('Percent (%)')
ax.margins(0, 0) # Set margins to avoid "whitespace"
plt.show()
This gives the output shown below.
这将给出如下所示的输出。
#1
17
A simple way to achieve this is to make sure that for every x-value, the y-values sum to 100.
实现这一点的一个简单方法是确保对于每一个x值,y值之和为100。
I assume that you have the y-values organized in an array as in the example below, i.e.
我假设你有一个数组中的y值,如下面的例子所示。
y = np.array([[17, 19, 5, 16, 22, 20, 9, 31, 39, 8],
[46, 18, 37, 27, 29, 6, 5, 23, 22, 5],
[15, 46, 33, 36, 11, 13, 39, 17, 49, 17]])
To make sure the column totals are 100, you have to divide the y
array by its column sums, and then multiply by 100. This makes the y-values span from 0 to 100, making the "unit" of the y-axis percent. If you instead want the values of the y-axis to span the interval from 0 to 1, don't multiply by 100.
为了确保列总数为100,必须将y数组除以它的列和,然后再乘以100。这使得y值跨度从0到100,使y轴的“单位”为百分比。如果你想让y轴的值跨度从0到1,不要乘以100。
Even if you don't have the y-values organized in one array as above, the principle is the same; the corresponding elements in each array consisting of y-values (e.g. y1
, y2
etc.) should sum to 100 (or 1).
即使没有如上所述在一个数组中组织y值,原理也是一样的;每个数组中包含y值(如y1, y2等)的对应元素之和应该是100(或1)。
The below code is a modified version of the example @LogicalKnight linked to in his comment.
下面的代码是经过修改的@LogicalKnight示例的一个版本。
import numpy as np
from matplotlib import pyplot as plt
fnx = lambda : np.random.randint(5, 50, 10)
y = np.row_stack((fnx(), fnx(), fnx()))
x = np.arange(10)
# Make new array consisting of fractions of column-totals,
# using .astype(float) to avoid integer division
percent = y / y.sum(axis=0).astype(float) * 100
fig = plt.figure()
ax = fig.add_subplot(111)
ax.stackplot(x, percent)
ax.set_title('100 % stacked area chart')
ax.set_ylabel('Percent (%)')
ax.margins(0, 0) # Set margins to avoid "whitespace"
plt.show()
This gives the output shown below.
这将给出如下所示的输出。