在Matplotlib直方图中定义bin宽度/x轴比例 [英] Defining bin width/x-axis scale in Matplotlib histogram

查看:529
本文介绍了在Matplotlib直方图中定义bin宽度/x轴比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib生成直方图.

I am generating histograms with matplotlib.

我需要使垃圾箱的宽度不相等,因为我最感兴趣的是最低的垃圾箱. 现在我正在这样做:

I need the bins to be of unequal width as I'm mostly interested in the lowest bins. Right now I'm doing this:

plt.hist(hits_array, bins = (range(0,50,10) + range(50,550,50)))

这将创建我想要的内容(前5个纸箱的宽度为10,其余50个),但是前五个纸箱当然比后一个纸箱窄,因为所有纸箱都显示在同一轴上

This creates what I want (the first 5 bins have a width of 10, the rest of 50), but the first five bins are, of course, narrower than the latter ones, as all bins are displayed on the same axis.

是否有一种方法可以影响x轴或直方图本身,因此我可以在前5个bin之后打破比例,以便所有bin均显示为相同宽度?

Is there a way to influence the x-axis or histogram itself so I can break the scale after the first 5 bins, so all bins are displayed as equally wide?

(我意识到这会创建一个扭曲的视图,我很满意,尽管我不介意在轴的两个不同比例的部分之间留出一些空间.)

(I realize that this will create a distorted view, and I'm fine with that, though I wouldn't mind a bit of space between the two differently scaled parts of the axis.)

任何帮助将不胜感激. 谢谢!

Any help will be greatly appreciated. Thanks!

推荐答案

您可以使用bar,而无需拆分轴.这是一个例子,

You can use bar and there is no need to split the axis. Here is an example,

import matplotlib.pylab as plt
import numpy as np

data = np.hstack((np.random.rand(1000)*50,np.random.rand(100)*500))
binwidth1,binwidth2=10,50
bins=range(0,50,binwidth1)+range(50,550,binwidth2)

fig,(ax) = plt.subplots(1, 1)

y,binEdges=np.histogram(data,bins=bins)

ax.bar(0.5*(binEdges[1:]+binEdges[:-1])[:5], y[:5],width=.8*binwidth1,align='center')
ax.bar(0.5*(binEdges[1:]+binEdges[:-1])[5:], y[5:],width=.8*binwidth1,align='center')
plt.show()

如果您真的想分割轴,请在此处.

In case you really want to split the axis have a look here.

这篇关于在Matplotlib直方图中定义bin宽度/x轴比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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