Pixel
Pixel

Reputation: 369

Make histogram in python with list

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.

Upvotes: 1

Views: 3325

Answers (1)

user3494103
user3494103

Reputation:

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

Upvotes: 4

Related Questions