当前位置:首页python > 正文

Python百分比直方图

作者:野牛程序员:2023-08-26 12:42:07python阅读 2618

以下是使用 Python 的 Matplotlib 库创建百分比直方图的示例代码。在这个示例中,假设已经有一组数据,想要将其转化为百分比直方图。

import matplotlib.pyplot as plt

# 假设您有一组数据
data = [20, 30, 40, 15, 25, 10, 50, 30, 15, 5]

# 将数据转换为百分比
total = sum(data)
percentage_data = [(value / total) * 100 for value in data]

# 创建直方图
plt.bar(range(len(percentage_data)), percentage_data)

# 设置 x 轴刻度和标签
plt.xticks(range(len(percentage_data)), ['Label1', 'Label2', 'Label3', 'Label4', 'Label5', 'Label6', 'Label7', 'Label8', 'Label9', 'Label10'])

# 设置 y 轴标签
plt.ylabel('Percentage')

# 显示图形
plt.show()

请注意,这只是一个基本示例,需要根据自己的数据和需求进行调整。在这个示例中,首先将原始数据转换为百分比,并使用 Matplotlib 的 bar 函数创建了一个简单的百分比直方图。然后,通过 xticks 设置 x 轴刻度和标签,通过 ylabel 设置 y 轴标签,最后使用 show 函数显示图形。



野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击