如何在0,1之间绘制y?

时间:2014-05-07 04:39:36

标签: python numpy

我试图在(0,1)之间或(0和1之间)之间绘制y 我试过很多东西但是我的直方图很糟糕,如果你认为有 你能帮助我一个方法吗,我使用了matplotlib.pyplot.ylim(0.0,1)但是没有用

代码是

import numpy
import numpy as np
import matplotlib.pyplot
import pylab
data = []


for j in range(40000):
    totalX= 0
    totalY = 0
    for i in range (5):

        # calculate order
        X = numpy.random.gamma(5.0)
        totalX = totalX+ X


        # calculate production
        Y = min(7.26, X)

        totalY = totalY + Y

        # calcculate new inventory
        #inventory = inventory + production - order
       # if inventory < 0:
         #   inventory = 0

    # calculate fill rate for last 5 orders
    fr = float(totalY) / float(totalX)
    if fr > 1:
        fr = 1

    # append FR to dataset
    data.append(fr)




matplotlib.pyplot.hist(data, normed = True, bins =30, align = 'right')
#matplotlib.pyplot.xticks(([.75, .80, .85, .90, .95, 1]))
matplotlib.pyplot.xlim(.70, 1.009)

#matplotlib.pyplot.minorticks_on()
matplotlib.pyplot.tick_params(axis='both', length = 10)
matplotlib.pyplot.axvline(numpy.mean(data), color = 'r')
matplotlib.pyplot.xlabel('Fill Rate')
matplotlib.pyplot.ylabel('Probability')
pylab.show()

1 个答案:

答案 0 :(得分:1)

当你说“我试图在(0,1)之间或(0和1比1之间)之间绘制y”时,你的意思是你希望这些条总结为1?

在这种情况下,您应该考虑更换

    matplotlib.pyplot.hist(data, normed = True, bins =30, align = 'right')

    weights = np.ones_like(data)/len(data)
    matplotlib.pyplot.hist(data, weights=weights)

如此处所示:plotting histograms whose bar heights sum to 1 in matplotlib

注意:Normed = True允许你使条形积分等于1(而不是条形的总和)