绘制来自txt文件的一个coloumn数据的直方图

时间:2016-06-01 10:52:04

标签: python numpy matplotlib

我需要从只有一列的文件中读取数据。然后绘制数据的禁止直方图。但是,通过以下代码,该图仅向我显示零线,仅此而已。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib

f= np.loadtxt('data Ties', unpack='False')

bins = np.linspace(0, 50, 100000)

plt.hist(f, bins, histtype='bar', rwidth=0.8)
plt.xlabel('Diameter')
plt.ylabel('Number of Chondrules')
plt.title('Distribution of chondules diameter')
plt.legend()
plt.show()

这是我的数据:

168000
199300
120900
216900
200800
137800
214200
174600
48200
126500
58700
149500
47500
5600
178500
25400
163000
182000
51900
66700
90300
210600
117800
164000
215200
170000
182000
38800
72700
161200

2 个答案:

答案 0 :(得分:0)

你向linspace反向提出了论据 - 它被定义为linspace(start, end, n),所以你应该写

bins = np.linspace(0, 100000, 50)

答案 1 :(得分:0)

需要设置适当数量的垃圾箱,即:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib

f= np.loadtxt('data', unpack='False')

# set bins' interval for your data
# You have following intervals: 
# 1st col is number of data elements in [0,10000);
# 2nd col is number of data elements in [10000, 20000); 
# ...
# last col is number of data elements in [100000, 200000]; 
bins = [0,10000,20000,50000,100000,200000] 

plt.hist(f, histtype='bar', bins = bins)
plt.xlabel('Diameter')
plt.ylabel('Number of Chondrules')
plt.title('Distribution of chondules diameter')
plt.legend()
plt.show()

enter image description here