python给定bined数据的简单直方图 [英] python plot simple histogram given binned data

查看:134
本文介绍了python给定bined数据的简单直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有计数数据(其中有100个),每个数据对应一个bin(0到99).我需要将这些数据绘制为直方图.但是,直方图会对这些数据进行计数,并且由于我的数据已被合并,因此无法正确绘制.

I have count data (a 100 of them), each correspond to a bin (0 to 99). I need to plot these data as histogram. However, histogram count those data and does not plot correctly because my data is already binned.

import random
import matplotlib.pyplot as plt
x = random.sample(range(1000), 100)
xbins = [0, len(x)]
#plt.hist(x, bins=xbins, color = 'blue') 
#Does not make the histogram correct. It counts the occurances of the individual counts. 

plt.plot(x)
#plot works but I need this in histogram format
plt.show()

推荐答案

如果我理解要正确实现的目标,则以下内容应为您提供所需的目标:

If I'm understanding what you want to achieve correctly then the following should give you what you want:

import matplotlib.pyplot as plt
plt.bar(range(0,100), x)
plt.show()

它不使用hist(),但看起来您已经将数据放入了垃圾箱,因此没有必要.

It doesn't use hist(), but it looks like you've already put your data into bins so there's no need.

这篇关于python给定bined数据的简单直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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