具有频率和计数的 matplotlib 直方图
matplotlib histogram with frequency and counts
我有数据(来自 space 分隔的两列文本文件)已经装箱但宽度仅为 1。我想将此宽度增加到大约 5。我如何使用numpy/matplotlib 在 Python 中?
正在使用,
data = loadtxt('file.txt')
x = data[:, 0]
y = data[:, 1]
plt.bar(x,y)
创建了太多条并使用,
plt.hist(data)
没有正确绘制直方图。我想我不明白 matplotlib 的直方图绘图是如何工作的。
查看下面的一些数据。
264 1
265 1
266 4
267 2
268 2
269 2
270 2
271 2
272 5
273 3
274 2
275 6
276 7
277 3
278 7
279 5
280 9
281 4
282 8
283 11
284 9
285 15
286 19
287 11
288 12
289 10
290 13
291 18
292 20
293 14
294 15
如果您在使用 plt.bar
之前使用 numpy.reshape
转换数据会怎样,例如:
In [83]: import numpy as np
In [84]: import matplotlib.pyplot as plt
In [85]: data = np.array([[1,2,3,4,5,6], [4,3,8,9,1,2]]).T
In [86]: data
Out[86]:
array([[1, 4],
[2, 3],
[3, 8],
[4, 9],
[5, 1],
[6, 2]])
In [87]: y = data[:,1].reshape(-1,2).sum(axis=1)
In [89]: y
Out[89]: array([ 7, 17, 3])
In [91]: x = data[:,0].reshape(-1,2).mean(axis=1)
In [92]: x
Out[92]: array([ 1.5, 3.5, 5.5])
In [96]: plt.bar(x, y)
Out[96]: <Container object of 3 artists>
In [97]: plt.show()
我不是 matplotlib 方面的专家,但我发现 hist
非常有用。 matplotlib site 上的示例很好地概述了一些功能。
我不知道如何在不转换的情况下使用您提供的示例数据。我更改了您的示例以在创建直方图之前对这些数据进行去量化。
我使用 this question's first answer 计算了 bin 大小。
import matplotlib.pyplot as plt
import numpy as np
data = np.loadtxt('file.txt')
dequantized = data[:,0].repeat(data[:,1].astype(int))
dequantized[0:7]
# Each row's first column is repeated the number of times found in the
# second column creating a single array.
# array([ 264., 265., 266., 266., 266., 266., 267.])
def bins(xmin, xmax, binwidth, padding):
# Returns an array of integers which can be used to represent bins
return np.arange(
xmin - (xmin % binwidth) - padding,
xmax + binwidth + padding,
binwidth)
histbins = bins(min(dequantized), max(dequantized), 5, 5)
plt.figure(1)
plt.hist(dequantized, histbins)
plt.show()
显示的直方图如下所示。
希望这个例子有用。
我有数据(来自 space 分隔的两列文本文件)已经装箱但宽度仅为 1。我想将此宽度增加到大约 5。我如何使用numpy/matplotlib 在 Python 中?
正在使用,
data = loadtxt('file.txt')
x = data[:, 0]
y = data[:, 1]
plt.bar(x,y)
创建了太多条并使用,
plt.hist(data)
没有正确绘制直方图。我想我不明白 matplotlib 的直方图绘图是如何工作的。
查看下面的一些数据。
264 1
265 1
266 4
267 2
268 2
269 2
270 2
271 2
272 5
273 3
274 2
275 6
276 7
277 3
278 7
279 5
280 9
281 4
282 8
283 11
284 9
285 15
286 19
287 11
288 12
289 10
290 13
291 18
292 20
293 14
294 15
如果您在使用 plt.bar
之前使用 numpy.reshape
转换数据会怎样,例如:
In [83]: import numpy as np
In [84]: import matplotlib.pyplot as plt
In [85]: data = np.array([[1,2,3,4,5,6], [4,3,8,9,1,2]]).T
In [86]: data
Out[86]:
array([[1, 4],
[2, 3],
[3, 8],
[4, 9],
[5, 1],
[6, 2]])
In [87]: y = data[:,1].reshape(-1,2).sum(axis=1)
In [89]: y
Out[89]: array([ 7, 17, 3])
In [91]: x = data[:,0].reshape(-1,2).mean(axis=1)
In [92]: x
Out[92]: array([ 1.5, 3.5, 5.5])
In [96]: plt.bar(x, y)
Out[96]: <Container object of 3 artists>
In [97]: plt.show()
我不是 matplotlib 方面的专家,但我发现 hist
非常有用。 matplotlib site 上的示例很好地概述了一些功能。
我不知道如何在不转换的情况下使用您提供的示例数据。我更改了您的示例以在创建直方图之前对这些数据进行去量化。
我使用 this question's first answer 计算了 bin 大小。
import matplotlib.pyplot as plt
import numpy as np
data = np.loadtxt('file.txt')
dequantized = data[:,0].repeat(data[:,1].astype(int))
dequantized[0:7]
# Each row's first column is repeated the number of times found in the
# second column creating a single array.
# array([ 264., 265., 266., 266., 266., 266., 267.])
def bins(xmin, xmax, binwidth, padding):
# Returns an array of integers which can be used to represent bins
return np.arange(
xmin - (xmin % binwidth) - padding,
xmax + binwidth + padding,
binwidth)
histbins = bins(min(dequantized), max(dequantized), 5, 5)
plt.figure(1)
plt.hist(dequantized, histbins)
plt.show()
显示的直方图如下所示。
希望这个例子有用。