我将csv文件作为python中的列表导入:
csv文件:
2012,3,22
2012,3,30
2012,4,4
2012,4,7
2012,3,22
2012,3,22
2012,3,27
2012,3,30
2012,3,30
2012,4,7
代码:
import csv
with open('file.csv', 'rb') as f:
reader = csv.reader(f)
date_list = list(reader)
print date_list
输出是:
[['2012', '3', '22'], ['2012', '3', '27'], ['2012', '3', '30'], ['2012', '3', '30'], ['2012', '4', '7']]
现在我想用matplotlib绘制它。我的代码是here,但我不知道将我的数据应用到代码中以生成条形图
我需要数据中的年份和月份。你可以在example
中看到它答案 0 :(得分:1)
要将您的月度计数变为your previous question
中堆积条形图所需的格式您可以按如下方式转换和提取:
import numpy as np
a = [['2012', '3', '22'], ['2012', '3', '27'], ['2012', '3', '30'], ['2012', '3', '30'], ['2012', '4', '7'], ['2011', '2', '12'], ['2011', '2', '14'], ['2011', '10', '10']]
# convert all date parts to integers
a = [[int(e) for e in d] for d in a]
years = set(d[0] for d in a)
minyear, maxyear = min(years), max(years)
nyears = maxyear - minyear + 1
nmonths = 12
monthly_counts = np.zeros((nyears,nmonths))
for year,month,_ in a:
monthly_counts[year-minyear,month-1] += 1