gpt4 book ai didi

python - 在 Python 中绘制直方图

转载 作者:IT老高 更新时间:2023-10-28 20:50:16 26 4
gpt4 key购买 nike

我有两个列表,x 和 y。
x 包含字母 A-Z,Y 包含它们在文件中的频率。

我尝试研究如何在直方图中绘制这些值,但在理解如何绘制它方面没有成功。

n, bins, patches = plt.hist(x, 26, normed=1, facecolor='blue', alpha=0.75)

x 会是上述列表中的列表 x 吗?

最佳答案

hist 作用于一组值并计算并从中绘制直方图。在您的情况下,您已经预先计算了每个组(字母)的频率。要以直方图形式表示您的数据,请使用更好的 matplotlib bar:

import numpy as np
import matplotlib.pyplot as plt

alphab = ['A', 'B', 'C', 'D', 'E', 'F']
frequencies = [23, 44, 12, 11, 2, 10]

pos = np.arange(len(alphab))
width = 1.0 # gives histogram aspect to the bar diagram

ax = plt.axes()
ax.set_xticks(pos + (width / 2))
ax.set_xticklabels(alphab)

plt.bar(pos, frequencies, width, color='r')
plt.show()

enter image description here

关于python - 在 Python 中绘制直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5926061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com