【问题标题】:Plot Histogram in Python在 Python 中绘制直方图
【发布时间】:2011-08-21 00:41:59
【问题描述】:

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

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

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

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

【问题讨论】:

  • 你的问题里连问号都没有。但是看看 matplotlib。

标签: python frequency histogram


【解决方案1】:

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()

【讨论】:

  • 不推荐使用 + (width / 2) 的东西。只需使用ax.set_xticks(pos)
猜你喜欢
  • 1970-01-01
  • 2018-05-23
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
相关资源
最近更新 更多