如何使直方图列的宽度全部相同 [英] How to make the width of histogram columns all the same

查看:124
本文介绍了如何使直方图列的宽度全部相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理直方图时遇到了一些麻烦.我有两列的df,并将它们绘制为堆积的直方图.我将它们放入特定的垃圾箱中(请参见下面的代码),但是我想在最后制作一个大垃圾箱(4000-10000).但是,默认情况下,大容器的列宽很大.是否有办法使大容器的尺寸不大?即使所有列的x范围不均匀,宽度也一样吗?

I'm having a bit of trouble manipulating a histogram. I have a df with two columns and I'm plotting them as a stacked histogram. I'm putting them into specific bins (see code below) but I want to make one large bin at the end (4000-10000). However, by default the column width of the large bin is huge.. Is there a way to make this large bin not larger in size? For all of the columns to be the same width even if their x-range is uneven?

代码:

df.plot.hist(stacked=True, bins=[0,400,800,1200,1600,2000,2400,2800,3200,3600,4000,10000],normed= True)

谢谢!

根据建议,尝试给出示例数据集.粗略,但也许可以帮助说明问题.

Per advice, trying to give an example dataset. Crude but maybe it will help illustrate the problem..

df = pd.DataFrame(np.random.randint(0,4000,size=(100, 2)), columns=['A','B'])
df['A'].loc[85:89] = np.random.randint(5000,10000, size=5)
df.plot.hist(stacked=True, bins=[0,400,800,1200,1600,2000,2400,2800,3200,3600,4000,10000],normed= True)

推荐答案

使所有bin的大小相同,然后将数据裁剪到最后一个bin的右端.

Make all bins the same size, then clip your data to the right end of the last bin.

df = pd.DataFrame(np.random.randint(0,4000,size=(100, 2)), columns=['A','B'])
df['A'].loc[85:89] = np.random.randint(5000,10000, size=5)
bins = [0,400,800,1200,1600,2000,2400,2800,3200,3600,4000,4400]
df.clip(upper=4400).plot.hist(stacked=True, bins=bins, normed=True)

请注意,正如评论中指出的那样,这实际上不是直方图.您可能需要自定义标签,以反映最后一个纸箱实际上比它看起来大的事实.

Take into account that, as pointed in the comments, this is not really a histogram. You might want to customize the labels to reflect the fact that the last bin is actually larger than it looks.

这篇关于如何使直方图列的宽度全部相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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