2011-05-08 86 views
15

我有兩個列表,x和y。
x包含字母表A-Z,Y包含它們在文件中的頻率。在Python中繪製直方圖

我試圖研究如何繪製直方圖中的這些值,但沒有理解如何繪製它。

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

在上面提到的列表中,x會列出x嗎?

+0

甚至有沒有在你的問題一個問號。但檢查出matplotlib。 – Keith 2011-05-08 07:17:39

回答

44

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

+1

不推薦使用'+(width/2)'東西。只需使用'ax.set_xticks(pos)'。 – smoneck 2017-07-18 10:13:15