I have a word length_of_word | repetitions dictionary and I want to make a histogram of that like the one in link below using only python built in functions no numpy or anything like it.
http://dev.collabshot.com/show/723400/
Please help me out at least with some pointe开发者_C百科rs.
I guess you must have a dict
that looks like this one, right ?
>>> d = {1:1, 2:10, 3:10, 4:6, 5:5, 6:4, 7:2, 8:1}
>>> d
{1: 1, 2: 10, 3: 10, 4: 6, 5: 5, 6: 4, 7: 2, 8: 1}
If so, I have a function that does the trick:
>>> def histo(dict_words):
# Get max values, plus delta to ease display
x_max = max(dict_words.keys()) + 2
y_max = max(dict_words.values()) + 2
# print line per line
print '^'
for j in range(y_max, 0, -1):
s = '|'
for i in range(1, x_max):
if i in dict_words.keys() and dict_words[i] >= j:
s += '***'
else:
s += ' '
print s
# print x axis
s = '+'
for i in range(1, x_max):
s += '---'
s += '>'
print s
# print indexes
s = ' '
for i in range(1, x_max):
s += ' %d ' % i
print s
>>> histo(d)
^
|
|
| ******
| ******
| ******
| ******
| *********
| ************
| ***************
| ***************
| ******************
|************************
+--------------------------->
1 2 3 4 5 6 7 8 9
>>>
Ok, there's a little more work to display values on the left and to format correctly numbers greater than 10 not to have shift in indexes, but I think it's a good start :-)
精彩评论