Histogram-Please Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • avenger3200
    New Member
    • Jun 2008
    • 1

    Histogram-Please Help

    Hello everyone,

    I am trying to make a histogram for a project in my class and I really need some help. Here is the question that my instructor provided:

    Create 1000 Random numbers between 0-100. Create a histogram of the values as a list. Make the bin range 10. Your book goes through a more complicated, but similar exercises.

    Here is the code that I have so far:

    import random
    list = []
    count = 0

    for i in range(1000):
    i = random.randrang e(0, 101, 1)
    print i
    list = list + [i]
    count = count + 1

    print 'The total number of random numbers is: \t', count

    Even with the help of my book I am unable to make the histogram.....c an someone PLEASE help me...its driving me crazy.
  • fordie1000
    New Member
    • Mar 2008
    • 32

    #2
    Hi,

    Not really sure from you post what type of random number generator that you
    need to use but just continuing on from what you were doing in your post with
    'random.randran ge' ... I put together this bit of code. You must have the
    extra python packages 'scipy' and 'Gnuplot.py' installed ... depending on your
    system?? you should be able to install these easily. Otherwise I guess you
    can calculate you histogram bins yourself (should only be a few lines of code)
    and you can always plot the histogram in an external package like Gnuplot
    which is standard on most *nix machines.

    Hope this helps you ...



    Code:
    import random
    import Gnuplot
    import scipy
    
    # Generate a list of random numbers
    
    hist=[]
    i=0
    while i < 1000:
        item = random.randrange(0,101,1)
        hist.append(item)
        i+=1
    
    # Generate your histogram
    
    (list,bins) = scipy.histogram(hist,bins=10)
    
    
    # Plot your histogram
    
    plot = Gnuplot.Gnuplot(persist=1)
    plot('set terminal x11 1 enhanced')
    plot.xlabel("No. of Bins")
    plot.ylabel("Count")
    dat = Gnuplot.Data(bins,list, with="histeps")
    plot.plot(dat)

    Comment

    Working...