matplotlib中的历史记录:轴未居中且轴上的比例不正确 [英] Hist in matplotlib: Bins are not centered and proportions not correct on the axis

查看:104
本文介绍了matplotlib中的历史记录:轴未居中且轴上的比例不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个例子:

 import matplotlib.pyplot as plt
 l = [3,3,3,2,1,4,4,5,5,5,5,5,5,5,5,5]
 plt.hist(l,normed=True)
 plt.show()

输出以图片形式发布.我有两个问题:

The output is posted as a picture. I have two questions:

a)为什么只有4个和5个容器位于其值的中心?其他人也应该不是吗?有没有技巧可以使它们居中?

a) Why are only the 4 and 5 bins centered around its value? Shouldn't the others be that as well? Is there a trick to get them centered?

b)为什么不将垃圾箱按比例归一化?我希望所有垃圾箱的y值总和为一个.

b)Why are the bins not normalised to proportion? I want the y values of all the bins to sum up to one.

请注意,我的实际示例在列表中包含更多的值,但它们都是离散的.

Note that my real example contains much more values in the list, but they are all discrete.

推荐答案

您应该调整plt.hist函数的关键字参数.其中有很多,文档可以帮助您回答其中许多问题问题.

You should adjust the keyword arguments of the plt.hist function. There are many of them and the documentation can help you answer many of these questions.

a. )您可以传递关键字bins=range(1,7)align=left.将bins关键字设置为序列将给出每个bin的边界.例如,[1,2], [2,3], [3,4], ..., [5, 6].

a. ) You can pass the keywords bins=range(1,7) and align=left. Setting the bins keyword to a sequence gives the borders of each bin. For example, [1,2], [2,3], [3,4], ..., [5, 6].

b. )检查纸槽宽度(rwidth!=1).从matplotlib.pyplot.hist文档:

b. ) Check your bin widths (rwidth!=1). From the matplotlib.pyplot.hist documentation:

如果为True,则返回元组的第一个元素为计数 归一化以形成概率密度,即n/(len(x)* dbin).在一个 概率密度,直方图的积分应为1;你 可以用梯形积分来验证概率 密度函数:

If True, the first element of the return tuple will be the counts normalized to form a probability density, i.e., n/(len(x)*dbin). In a probability density, the integral of the histogram should be 1; you can verify that with a trapezoidal integration of the probability density function:

这意味着您的垃圾箱下面的面积总和为1,但是由于垃圾箱宽度小于1,因此高度被归一化,以使高度之和不等于1.如果您调整rwidth=1,您会得到一个漂亮的情节:

This means that the area under your bins is summing up to one, but because the bin widths are less than 1, the heights get normalized in such a way that the heights don't add up to 1. If you adjust rwidth=1, you get a good looking plot:

plt.hist(l, bins=range(1,7), align='left', rwidth=1, normed=True)

这篇关于matplotlib中的历史记录:轴未居中且轴上的比例不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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