简体   繁体   English

箱内高度不均匀的直方图

[英]Histogram with uneven heights within bins

Why does my histogram look like this? 为什么我的直方图看起来像这样? I'm plotting it using the following code: 我正在使用以下代码进行绘制:

import matplotlib.pyplot as plt

n, bins, patches = plt.hist(data, 25)
plt.show()

where data contains about 500,000 sorted points. data包含约500,000个排序点。 Shouldn't each bin be smooth at the top? 每个垃圾箱的顶部都不应该光滑吗?

在此处输入图片说明

I suspect your data might not be a flat list, but, eg, a list of lists or similarly structured. 我怀疑您的数据可能不是平面列表,而是列表列表或类似结构的列表。 In that case, hist will bin by sublist. 在这种情况下, hist将按子列表进行分类。 This script demonstrates the difference: 该脚本演示了区别:

import random
from matplotlib import pyplot as plt

data = [[random.randint(0, x) for x in range(20)] for y in range(100)]

plt.subplot(211)
# structured
plt.hist(data, 25)

plt.subplot(212)
# flattened
plt.hist([i for l in data for i in l], 25)

plt.show()

You will probably have to use some kind of similar "flattening" on your data. 您可能必须在数据上使用某种类似的“扁平化”。

I think this example from the matplotlib gallery displays the kind of plot you want. 我认为matplotlib画廊中的这个示例显示了您想要的绘图类型。 Therefore, you should either use histtype="stepfilled" or histtype="bar" . 因此,您应该使用histtype="stepfilled"histtype="bar"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM