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
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
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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
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
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