我需要使用matplotlib在Python中制作直方图。我的数据是每月发生频率的元组,如下所示,其中x轴应显示月份,y轴应显示频率。有人可以帮忙吗?
state = {
todos: [
{ id: 432402, title: "Make some music", isDone: false },
{ id: 421402, title: "Conquer the world", isDone: false },
{ id: 427740, title: "Go to the mall", isDone: true },
{ id: 471402, title: "Do some homework", isDone: false }
]
};
答案 0 :(得分:0)
如果您想要相对于月份的直方图,则应执行以下操作:
import calendar
dmonths = dict((v,k) for k,v in enumerate(calendar.month_abbr))
import numpy as np
from matplotlib import pyplot as plt
list1 = [('Jun-07', 10),
('Jun-08', 15),
('Jun-09', 16),
('Nov-07', 17),
('Nov-08', 16),
('Nov-09', 14),
('May-11', 16),
('May-10', 18),
('May-13', 14),
('May-12', 14),
('May-14', 12),
('Jun-14', 10),
('Jun-11', 14),
('Jun-10', 19),
('Jun-13', 13),
('Jun-12', 14),
('Feb-09', 10),
('Nov-14', 10),
('Nov-13', 12),
('Nov-12', 13)]
list2 = [dmonths[x[0][:3]] for x in list1]
list3 = [x[1] for x in list1]
plt.hist(np.array(list2), bins=np.array(range(1,12)), weights=np.array(list3))
前两行给出了从月份到整数的查找表。然后,您只需要提取月份的名称,将它们转换为整数,然后绘制直方图,并将值作为权重即可。
答案 1 :(得分:0)
import matplotlib.pyplot as plt
def histogram():
set_list = set(link_list)
return [(idx, link_list.count(idx)) for idx in set_list]
xx, yy = [datetime.strptime(idx[0],'%b-%y') for idx in his_sorted],
[idx[1] for idx in his_sorted]
plt.bar(xx, yy, width = 50)