creating histogram using python code only.

This is the place for queries that don't fit in any of the other categories.

creating histogram using python code only.

Postby zahrein » Sun Nov 24, 2013 1:44 pm

At the moment, i have problem on creating histogram. I am stuck. Here is a code that i can generate random number.
Code: Select all
def _Read_Random_(start, end):
    import random
    b=[]
    for i in range (10):
        a=random.randint(start, end)
        print a
        b.append(a)
        print b
        #calculating the number of the list in gthe array
        c=len(b)


Now, the next step, How do i count the occurrence value /frequency value tin the array and display it in a histogram form.

Appreciate all the helps
zahrein
 
Posts: 15
Joined: Wed Apr 03, 2013 1:45 am

Re: creating histogram using python code only.

Postby rrashkin » Sun Nov 24, 2013 2:43 pm

Let's look at a general problem. Take a list of numbers:
Code: Select all
>>> lst1=range(12)+range(8)
>>> lst1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7]

There are many ways to find the unique elements but I like to use sets:
Code: Select all
>>> s1=set(lst1)
>>> s1
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

Now, again there are many ways but I would just use count() and iterate over the set:
Code: Select all
>>> lst2=[(i,lst1.count(i)) for i in s1]
>>> lst2
[(0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2), (8, 1), (9, 1), (10, 1), (11, 1)]
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: creating histogram using python code only.

Postby zahrein » Mon Nov 25, 2013 8:13 am

Thanks a Lot. I made it. Here is the histogram that i generated using the python code.

Code: Select all


lst1=range(10)+range(5)+range(5)+range(5)
lst1
print lst1[1]


#----------------------------------------------------------
# this function is to set
s1=set(lst1)
print s1
print "this is s1  =" + str(s1)
for i in s1:
    print i


#----------------------------------------------------------
#output of this count function->2

print lst1.count(0)

#----------------------------------------------------------
#making it into frequency format
lst2=[(i,lst1.count(i)) for i in s1]
# using the function count to get a fequency
#output of this function-[(0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2), (8, 1), (9, 1), (10, 1), (11, 1)]
print lst2
print len(lst2)

a=len(lst2)
print a

#---------------------------------------------------------------------------------------------------------------
# trying to display out for each element
for b in range (a):
    #print "hi"
    print lst2[b]
    the_multiplier=(lst2[b][1])


    print "code %s : %s" % (str(lst2[b]), str("*"*the_multiplier))
    #print "*"*the_multiplier



Thr output:

code (0, 4) : ****
(1, 4)
code (1, 4) : ****
(2, 4)
code (2, 4) : ****
(3, 4)
code (3, 4) : ****
(4, 4)
code (4, 4) : ****
(5, 1)
code (5, 1) : *
(6, 1)
code (6, 1) : *
(7, 1)
code (7, 1) : *
(8, 1)
code (8, 1) : *
(9, 1)
code (9, 1) : *
>>>
zahrein
 
Posts: 15
Joined: Wed Apr 03, 2013 1:45 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests