用python画面积图_使用matplotlib创建100%堆积面积图

实现这一点的一个简单方法是确保每个x值的y值总和为100。

我假设您将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]])

要确保列总数为100,必须将y数组除以其列和,然后乘以100。这使得y值的范围从0到100,使得y轴的“单位”百分比。如果您希望y轴的值跨越0到1之间的间隔,请不要乘以100。

即使没有像上面那样在one数组中组织y值,原理也是一样的;每个数组中由y值组成的相应元素(例如y1,y2等)的总和应该是100(或1)。

下面的代码是在他的注释中链接到的example@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()

这将产生如下所示的输出。

WO1Sf.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值