加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 具有频率和计数的matplotlib直方图

发布时间:2020-12-20 13:47:46 所属栏目:Python 来源:网络整理
导读:我有数据(来自空格分隔的文本文件有两列)已经装箱但宽度只有1.我想将此宽度增加到大约5.如何在 Python中使用numpy / matplotlib? 使用, data = loadtxt('file.txt')x = data[:,0]y = data[:,1]plt.bar(x,y) 创造了太多的酒吧和使用, plt.hist(data) 没有适
我有数据(来自空格分隔的文本文件有两列)已经装箱但宽度只有1.我想将此宽度增加到大约5.如何在 Python中使用numpy / matplotlib?

使用,

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,8,9,1,2]]).T

In [86]: data
Out[86]: 
array([[1,4],[2,3],[3,8],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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读