1

I need some help making a histogram in python

Got some code like this:

import csv
import matplotlib.pyplot as plt

path = []

with open('paths_finished.tsv','r') as tsv:
    paths = [line.strip().split('\n') for line in tsv]
    newPath = paths[16:]
    counter = 0


for k in range(0,len(newPath)):
    path = newPath[counter][0].count(';')
    counter +=1

    print path

if i print path, i get a list with numbers like (9, 5, 1, 3, 12 , 7, 5, 9) and so on. Now i have to make a histogram of path. Any can help with that?

UPDATE:

My tsv file contains this:

['36dabfa133b20e3c', '1249525912', '112', '14th_century;China;Gunpowder;Fire', '2']
['20418ff4797f96be', '1229188046', '139', '14th_century;Time;Isaac_Newton;Light;Color;Rainbow', '1']
['08888b1b428dd90e', '1232241510', '74', '14th_century;Time;Light;Rainbow', '3']
['08888b1b428dd90e', '1232241601', '167', '14th_century;15th_century;Plato;Nature;Ultraviolet;Color;Rainbow', 'NULL']
['4cb0068c36658716', '1248654953', '253',  '14th_century;Time;Science;Nature;Weather;Sunlight;     <;Sun;Earth%27s_atmosphere;Ultraviolet;Color;Light;Rainbow', '3']
['1082b6b8501a04b1', '1248791776', '218', '14th_century;Christianity;Bible;God;Nature;Earth%27s_atmosphere;Ultraviolet;Optical_fiber;L    ight;Rainbow', '3']
['390ae528683f78ab', '1250727252', '66', '14th_century;Time;Astronomy;Light;Rainbow',   'NULL']
['0d57c8c57d75e2f5', '1283956474', '391', 

My path prints this:

12
6
4
4
6
6
4
4
8
4
4

and alot more numbers.

6
  • Whats inside your tsv file? Commented Apr 5, 2014 at 12:27
  • Alot, some paths for example Commented Apr 5, 2014 at 12:31
  • Can you add an example? Commented Apr 5, 2014 at 12:32
  • Yes. i will update my thread in a moment.. 2sec Commented Apr 5, 2014 at 12:35
  • We have only used the path you see on row[3] and counted how many path there are in each line and putted that into a list. Commented Apr 5, 2014 at 12:38

1 Answer 1

4

Hopefully this is what your looking for?

path2 = list(set(path)) ## gets the unique values in the list

histo = []

for i in path2:
    histo.append(path.count(i)) ## add the number of occurances to the histo list

plt.bar(path2, histo)
plt.show()

Giving this as the output: enter image description here

To add axis names you can add this code:

plt.suptitle('Title', fontsize=14)
plt.xlabel('Number', fontsize=12)
plt.ylabel('Freq', fontsize=12)

EDIT Based on the uploaded tsv file:

import csv
import matplotlib.pyplot as plt

path = []

with open('paths_finished.tsv','r') as tsv:
    paths = [line.strip().split('\n') for line in tsv]
    newPath = paths[16:]
    counter = 0

items = []
for k in range(0,len(newPath)):
    path = newPath[counter][0].count(';')
    counter +=1
    items.append(path)
    print path

print items


path2 = list(set(items)) ## gets the unique values in the list

histo = []

for i in path2:
    histo.append(items.count(i)) ## add the number of occurances to the histo list

plt.bar(path2, histo)
plt.suptitle('Title', fontsize=14)
plt.xlabel('Number', fontsize=12)
plt.ylabel('Freq', fontsize=12)
plt.show()

Giving the output: enter image description here

Sign up to request clarification or add additional context in comments.

15 Comments

No add it after the print path code you have. I'm not sure if this is what you want though? Are you wanting to plot the frequency of the numbers in path? or plot the numbers in path against the first column of the tsv file?
just want to plot the frequency of the numbers in path with a histogram.
What line is it coming from?
from this one: path2 = list(set(path)) ## gets the unique values in the list
What does your 'print path' output?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.