栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用matplotlib创建100%堆积面积图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用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。

即使您没有将y值组织在上面的 一个 数组中,原理也是一样的。每个数组中由y值组成的相应元素(例如

y1
y2
等等)应加起来为100(或1)。

下面的代码是@LogicalKnight示例的修改版本,该示例链接到他的注释中。

import numpy as npfrom matplotlib import pyplot as pltfnx = 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 divisionpercent = y /  y.sum(axis=0).astype(float) * 100fig = 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()

这给出了如下所示的输出。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645934.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号