954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Bar Graph

How would you make a simple bar graph from a list of data?

data = [20, 15, 10, 7, 5, 4, 3, 2, 1, 1, 0]
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Hi!

for i in data:
    print "#" * i

:)

Regards, mawe

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

Gruess Gott mawe!
Your solution is very simple and very sweet! The elegance of Python keeps amazing an old programmer like me.

Here I was thinking Tkinter and rectangles. Well, I am almost done with that much more fancy/complicated solution. I guess I will just stick it into the code snippets.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Wow. Genius, Mawe.

LaMouche
Posting Whiz in Training
269 posts since Oct 2006
Reputation Points: 83
Solved Threads: 39
 

Thanks mawe, that will come in handy! Also thanks to vegaseat for the beauty of a bar graph made with Tkinter, that is my first intro to Tkinter. Looks like GUI programming is much simpler than with other languages.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

I tried to transpose mawe's horizontal simple bargraph into a vertical simple bargraph, but with no success. Has anybody tried this before?

Make something like this:

##########
#####
##
#


Look like this:

#
#
#
#
#
# #
# #
# #
# # #
# # # #
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Hi!

def ascii_barchart( lst ):
    for row in range( max(lst), 0, -1 ):
        for elem in lst:
            if elem >= row: 
                print "# ",
            else: 
                print "  ",
        print

l = [20, 10, 15, 5]
ascii_barchart( l )

Not as simple as the first one, but ... still quite simple ;)

Regards, mawe

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

Mawe sure knows how to keep it simple and elegant! Here is my contribution to mawe's original horizontal bar graph, I added one line of actual data value markers to the end, still simple:

data = [20, 15, 10, 7, 5, 4, 3, 2, 1, 1, 0]

# pad each bar with spaces to total length of max(data), here 20
bar_list = []
for x in data:
    str1 = "#" * x
    # space pad to the right
    str2 = str1.ljust(max(data))
    bar_list.append(str2)

# test it, also show data value marker at end
for index, item in enumerate(bar_list):
    print item, '  -', data[index]
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

How do you mkae a bar graph were you enter 6 grades?

billking
Newbie Poster
2 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

if zero = true
then else if zero = false

Editors note: stop this gibberish or get banned!

billking
Newbie Poster
2 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Hi!

for i in data:
    print "#" * i

:)

Regards, mawe


:-O

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You