python 根据数据时间画柱型图
import datetime import time import pandas as pd import matplotlib.pyplot as plt data=pd.read_stata('pid去重后.dta') data1=data['post_time'].values.tolist() print(data1[0],data1[1]) list1=[] for t in data1: timeTuple=time.localtime(t/1e9) time1=time.strftime('%Y-%m-%d %H:%M:%S', timeTuple) list1.append(time1) data['post_time'] = list1 # print(data['post_time']) print(list1[0],list1[1]) list2=[] list3=[] list4=[] list5=[] data2=data['post_time'].values.tolist() print(data2) print("1",data2[0]) for m in data2: cut2=m[5:7] cut3=m[8:10] cut4=m[11:13] cut5=m[14:16] # print(cut2) # print(cut3) # print(cut4) # print(cut5) list2.append(cut2) list3.append(cut3) list4.append(cut4) list5.append(cut5) data['month']=list2 data['day']=list3 data['hour']=list4 data['min']=list5 print(data.head(0)) dict1={} data_hour=data['hour'].values.tolist() print(data_hour) for bh in data_hour: if bh in dict1: dict1[bh]+=1 else: dict1[bh]=1 dict2 = sorted(dict1.items(), key=lambda d: d[0]) print(dict2) keylist=[dict2[i][0] for i in range(len(dict2))] # print(keylist) vallist=[dict2[i][1] for i in range(len(dict2))] # print(vallist) df=pd.DataFrame(index=keylist,data=vallist,columns=["数量"]) print(df) plt.bar(keylist,vallist) plt.xlabel('时间',fontproperties="STSong") plt.ylabel('发帖数量',fontproperties="STSong") for a, b in zip(keylist,vallist): plt.text(a, b + 0.05, '%.0f' % b, ha='center', va='bottom', fontsize=10) plt.savefig('整体发帖小时分布.png' )