python数据可视化(三)读取csv数据绘制折线图绘制折线图
⽤折线图统计csv⽂件中的上⽹⾼峰时段
import csv
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
def hot_Time1():#统计上⽹⾼峰时段
#根据数据使⽤折线图
pr = pd.read_csv("hydata_swjl_0.csv")
pr1 = pd.read_csv("hydata_swjl_1.csv", low_memory=False)
print('⽤折线图统计上⽹⾼峰时段:')
on_time=[]
off_time=[]
for i in pr['ONLINETIME']:
on_time.append(i)
for i in pr1['ONLINETIME']:
on_time.append(i)
for i in pr['OFFLINETIME']:
off_time.append(i)
for i in pr1['OFFLINETIME']:
off_time.append(i)
time1_on = [] # 0~6点
time2_on = [] # 6~8点
time3_on = [] # 8~10点
time4_on = [] # 10~12点
time5_on = [] # 12~14点
time6_on = [] # 14~16点
time7_on = [] # 16~18点
time8_on = [] # 18~20点
time9_on = [] # 20~24点
time_on = [] #上机时间
time_off=[] #下机时间
time1_off = [] # 0~6点
time2_off = [] # 6~8点
time3_off = [] # 8~10点
time4_off = [] # 10~12点
time5_off = [] # 12~14点
time6_off = [] # 14~16点
time7_off = [] # 16~18点
time8_off = [] # 18~20点
time9_off = [] # 20~24点
for i in on_time:
t=0
t=(int(i/10000)%100)
time_on.append(t)
for i in off_time:
t=0
t=(int(i/10000)%100)
time_off.append(t)
for i in time_on:#上机时间统计
if 0 <=i < 6:
time1_on.append(i)
time1_on.append(i)
elif 6 <=i < 8:
time2_on.append(i)
elif 8 <= i < 10:
time3_on.append(i)
elif 10 <= i < 12:
time4_on.append(i)
elif 12 <= i < 14:
time5_on.append(i)
elif 14 <= i < 16:
time6_on.append(i)
elif 16 <= i < 18:
time7_on.append(i)
elif 18 <= i < 20:
time8_on.append(i)
elif 20 <= i < 24:
time9_on.append(i)
else:
time1_on.append('0')
for i in time_off:#下机时间统计
if 0 <=i < 6:
time1_off.append(i)
elif 6 <=i < 8:
time2_off.append(i)
elif 8 <= i < 10:
time3_off.append(i)
elif 10 <= i < 12:
time4_off.append(i)
elif 12 <= i < 14:
time5_off.append(i)
elif 14 <= i < 16:
time6_off.append(i)
elif 16 <= i < 18:
time7_off.append(i)
elif 18 <= i < 20:
time8_off.append(i)
elif 20 <= i < 24:
time9_off.append(i)
else:
time1_off.append('0')
values1_on = [len(time1_on),len(time2_on),len(time3_on),len(time4_on),len(time5_on), len(time6_on),len(time7_on),len(time8_on),len(time9_on)]
values1_off = [len(time1_off),len(time2_off),len(time3_off),len(time4_off),
len(time5_off),len(time6_off),len(time7_off),len(time8_off),len(time9_off)]
#绘制上机统计图
#plt.title('Peak Hours_1',fontsize=15)
plt.xlabel('Up and down time_1',color='blue')
plt.ylabel('Number of time periods_1',color='blue')
#plt.bar(index+bw, values1_on, bw, alpha=0.7)
x=['0~6','6~8','8~10','10~12','12~14','14~16','16~18','18~20','20~24']
plt.plot(x,values1_on,label='Onlinetime',color='y',linewidth=3.0)
plt.plot(x,values1_off,label='Offtime',color='b',linewidth=2.0)
python怎么读csv数据plt.plot(0,0)
plt.plot(0, 0)
plt.plot(0, 0)
plt.plot(0, 0)
plt.plot(0, 0)
plt.plot(0,0)
plt.plot(0, 0)
plt.plot(0, 0)
plt.plot(0, 0)
plt.plot(0, 0)
#icks()
# plt.legend(['0~6','6~8','8~10','10~12','12~14','14~16','16~18','18~20','20~24'], # loc=2,edgecolor='b')
plt.legend(loc='best')
plt.show()
print('可以看出,折线图可以更好地反映上⽹的⾼峰时期。')
print('早晨上⽹⼈数较少,⽽到了下午,上⽹⼈数陡增')
print()
if __name__=="__main__":
hot_Time1()
结果:
注:在这⾥是表⽰的两条折线,如只需要⼀条,那么在
plt.plot()这⾥添加⼀条线的数值即可
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论