使用列表在python中创建直方图

时间:2014-04-05 12:20:17

标签: python matplotlib

我需要一些帮助在python中制作直方图

得到一些像这样的代码:

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

如果我打印路径,我会得到一个包含数字的列表,如(9,5,1,3,12,7,5,9),依此类推。 现在我必须制作路径直方图。有人可以帮忙吗?

更新:

我的tsv文件包含:

['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', 

我的路径打印出来:

12
6
4
4
6
6
4
4
8
4
4

还有更多数字。

1 个答案:

答案 0 :(得分:2)

希望这是你想要的?

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()

将此作为输出: enter image description here

要添加轴名称,您可以添加以下代码:

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

编辑根据上传的tsv文件:

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()

给出输出: enter image description here