有没有办法在numpy.hist回到同一长度的数组? [英] Is there a way to return same length arrays in numpy.hist?
问题描述
我想在Python中创建直方图,一些自定义的值正常化y轴上的值。对于这一点,我想这样做是这样的:
I'm trying to create a histogram plot in python, normalizing with some custom values the y-axis values. For this, I was thinking to do it like this:
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('foo.bar')
fig = plt.figure()
ax = fig.add_subplot(111)
hist=np.histogram(data, bins=(1.0, 1.5 ,2.0,2.5,3.0))
x=[hist[0]*5,hist[1]]
ax.plot(x[0], x[1], 'o')
,但当然,最后一行给出:
but of course, the last line gives:
ValueError: x and y must have same first dimension
有没有办法强制np.hist给了X [0]和X相同数量的元素[1]的阵列,例如通过删除了其中一人的第一个或最后一个元素?
Is there a way to force np.hist to give the same number of elements for the x[0] and x[1] arrays, for example by deleting the first or last element for one of them?
推荐答案
HIST [1]包含在其中所做的直方图的限制。我猜你可能想获得这些区间的中心,是这样的:
hist[1] contains the limits in which you have made the histogram. I guess you probably want to get the centers of those intervals, something like:
x = [hist[0], 0.5*(hist[1][1:]+hist[1][:-1])]
然后剧情应该没问题吧?
and then the plot should be ok, right?
这篇关于有没有办法在numpy.hist回到同一长度的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!