用Python绘制直方图 [英] Plot Histogram in Python

查看:191
本文介绍了用Python绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two lists, x and y.
x contains the alphabet A-Z and Y contains the frequency of them in a file.

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

I've tried researching how to plot these values in a histogram but has had no success with understanding how to plot it.

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

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

Would x be list x in the lists mentioned above?

推荐答案

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

hist works on a collection of values and computes and draws the histogram from them. In your case you already precalculated the frequency of each group (letter). To represent your data in an histogram form use better 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()

这篇关于用Python绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆