0

I am trying to plot y between (0, 1) or between (0 and lees than 1) I tried by many things but the I have bad histogram, if do you think there is a way would you please help me, I used matplotlib.pyplot.ylim(0.0, 1) but doesn't work

the code is

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()
Tim
  • 41,901
  • 18
  • 127
  • 145
user3462393
  • 51
  • 3
  • 9
  • Clipping a histogram plot on the y axis to (0,1) makes no sense because histograms count events, and usually you'll have more than 1 event in many of the bins. It sounds like you want to scale the histogram to divide the number of events by the total number of events. – Paul May 07 '14 at 06:09
  • It depends on whether you need an estimate of the frequency of a bin or the Cumulative Distribution Function that is basically the integral thereof. As I suggested, dividing the histogram by 1/#events will give you the frequency of the bins, and `pandas.Series.quantile` makes a good inverse CDF. – Paul May 07 '14 at 06:33

1 Answers1

2

When you say "I am trying to plot y between (0, 1) or between (0 and lees than 1)", you mean that you want the bars to sum up to 1 ?

In that case, you should consider replacing

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

with

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

as seen here: plotting histograms whose bar heights sum to 1 in matplotlib

Note: Normed = True allows you to have the integral over the bars equals to 1 (rather than the sum of the bars)

Community
  • 1
  • 1
7hibault
  • 2,371
  • 3
  • 23
  • 33