绘图(饼图/折线图设置)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  5 17:21:10 2019

@author: didi
"""

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 26 11:19:05 2019

@author: didi
"""

#coding:utf-8

import os
import sys
import matplotlib.pyplot as plt
import pandas as pd
import time
import datetime
import urllib, urllib.request
from matplotlib import pyplot as plt
import numpy as np


# 二维 list 生成 html table
def html_table(title, data):
    content = '<h2>' + title + '</h2>\n'
    content += '''<table border=\"1\" cellpadding=\"4\" cellspacing=\"1\" style=\"border-collapse:collapse;text-align:center;font-size:14px;white-space:nowrap;\">'''
    for dat in data:
        td = '<td>'
        if data.index(dat) == 0:
            td = '''<td style=\"background: #2b71bb;color:#fff;\" >'''
        content += '<tr>'
        for d in dat:
            content += td
            content += str(d)
            content += '</td>\n'
        content += '</tr>\n'
    content += '</table>\n'
    return content


# 处理数据
dt = '2019/07/20'
df = pd.read_csv('index.csv',encoding='gbk')
df.columns
date = datetime.datetime.strptime(dt, '%Y/%m/%d')
data = [['日期','运营车辆数','完成工单数','剩余工单数','总工单数','工单完成率','工单准确率','模型准确率','标签消除数','标签生成数',
         '用户上报数','车辆上报数','总订单数','用户上报率','车辆上报率','用户上报刹车率','用户上报无动力率',
         '短时订单率','低速订单率','疑似故障订单率','疑似故障短时订单率','疑似故障低速订单率'
         ,'有效车均单','无动力cpo数','刹车cpo数','质量cpo数']]
#全国
c_df= df[df['city_id'] == 0]

for i in range(6):
    pre_dt = (date - datetime.timedelta(days=5-i)).strftime('%Y/%m/%d')
    pre_df = c_df[c_df['pt'] == pre_dt]
    pre_df.reset_index(inplace=True)
    print (pre_df)
    print (pre_dt)
    tmp = []
    tmp.append(pre_dt)
    tmp.append(pre_df.loc[0,'opera_vehicle_cnt'])
    tmp.append(pre_df.loc[0,'workord_fin_num'])
    tmp.append(pre_df.loc[0,'workord_left_num'])
    tmp.append(pre_df.loc[0,'workord_total_num'])
    tmp.append(str(round(float(pre_df.loc[0,'workorder_fin_ratio'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'workorder_precision'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'precision'])*100,2))+'%')
    tmp.append(pre_df.loc[0,'remove_cnt'])
    tmp.append(pre_df.loc[0,'create_cnt'])
    tmp.append(pre_df.loc[0,'passenger_report_cnt'])
    tmp.append(pre_df.loc[0,'vehicle_report_cnt'])
    tmp.append(pre_df.loc[0,'total_cnt'])
    tmp.append(str(round(float(pre_df.loc[0,'passenger_report_ratio'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'vehicle_report_ratio'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'passenger_brake_report_ratio'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'passenger_power_report_ratio'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'short_time_ord_ratio'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'low_speed_ord_ratio'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'fault_label_ord_ratio'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'fault_label_short_time_ord_ratio'])*100,2))+'%')
    tmp.append(str(round(float(pre_df.loc[0,'fault_label_low_speed_ord_ratio'])*100,2))+'%')
    tmp.append(round(pre_df.loc[0,'real_avg_ord_cnt'],2))
    tmp.append(pre_df.loc[0,'nopower_cpo_num'])
    tmp.append(pre_df.loc[0,'brake_cpo_num'])    
    tmp.append(pre_df.loc[0,'quality_cpo_num'])    
    data.append(tmp)
content = html_table('全国报修策略汇总统计', data)
#各城市
s_df = df[df['city_id'] != 0]
data = [['城市','日期','运营车辆数','完成工单数','剩余工单数','总工单数','工单完成率','工单准确率','模型准确率','标签消除数','标签生成数',
         '用户上报数','车辆上报数','总订单数','用户上报率','车辆上报率','用户上报刹车率','用户上报无动力率',
         '短时订单率','低速订单率','疑似故障订单率','疑似故障短时订单率','疑似故障低速订单率'
         ,'有效车均单','无动力cpo数','刹车cpo数','质量cpo数']]
df = s_df[(s_df['pt'] == dt) & (s_df['remove_cnt'] > 0)].groupby(['city_name']).sum()
print ("!!!")
print (df)
df.reset_index(inplace=True)
df = df.sort_values(by=['total_cnt'],ascending= False)
df.reset_index(inplace=True)
print (df)
for i in range(df.shape[0]):
    tmp = []
    tmp.append(df.loc[i,'city_name'])
    tmp.append(dt)
    tmp.append(df.loc[i,'opera_vehicle_cnt'])
    tmp.append(df.loc[i,'workord_fin_num'])
    tmp.append(df.loc[i,'workord_left_num'])
    tmp.append(df.loc[i,'workord_total_num'])
    tmp.append(str(round(float(df.loc[i,'workorder_fin_ratio'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'workorder_precision'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'precision'])*100,2))+'%')
    tmp.append(df.loc[i,'remove_cnt'])
    tmp.append(df.loc[i,'create_cnt'])
    tmp.append(df.loc[i,'passenger_report_cnt'])
    tmp.append(df.loc[i,'vehicle_report_cnt'])
    tmp.append(df.loc[i,'total_cnt'])
    tmp.append(str(round(float(df.loc[i,'passenger_report_ratio'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'vehicle_report_ratio'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'passenger_brake_report_ratio'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'passenger_power_report_ratio'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'short_time_ord_ratio'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'low_speed_ord_ratio'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'fault_label_ord_ratio'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'fault_label_short_time_ord_ratio'])*100,2))+'%')
    tmp.append(str(round(float(df.loc[i,'fault_label_low_speed_ord_ratio'])*100,2))+'%')
    tmp.append(round(df.loc[i,'real_avg_ord_cnt'],2))
    tmp.append(df.loc[i,'nopower_cpo_num'])
    tmp.append(df.loc[i,'brake_cpo_num'])    
    tmp.append(df.loc[i,'quality_cpo_num'])
    data.append(tmp)
content += '<br></br>\n'
print (len(data))
for d in data:
    print (len(d))
content += html_table('各城市报修策略汇总统计', data)

pic1 = pd.read_csv('pic1.csv',encoding = 'gbk')
pic1['上报内容'] = '刹车'
for i in range(0,len(pic1)):
    if pic1['report_name'][i] in ['车头','车头变形']:
        pic1['上报内容'][i] = '车头'
    elif pic1['report_name'][i] in ['转把故障/破坏','转把']:
        pic1['上报内容'][i] = '转把'
    elif pic1['report_name'][i] in ['前灯不亮','前灯']:
        pic1['上报内容'][i] = '前灯'
    elif pic1['report_name'][i] in ['座椅','鞍座破坏/故障']:
        pic1['上报内容'][i] = '座椅'
    elif pic1['report_name'][i] in ['脚撑损坏','脚撑','脚支撑故障']:
        pic1['上报内容'][i] = '脚撑'
    elif pic1['report_name'][i] in ['链条','链条损坏/脱落']:
        pic1['上报内容'][i] = '链条'
    elif pic1['report_name'][i] in ['挡泥板']:
        pic1['上报内容'][i] = '挡泥板'
    elif pic1['report_name'][i] in ['电池','骑行无动力','整车无电','骑行中断电']:
        pic1['上报内容'][i] = '无动力'
    elif pic1['report_name'][i] in ['开关锁','电池锁故障']:
        pic1['上报内容'][i] = '开关锁'
    elif pic1['report_name'][i] in ['车轮']:
        pic1['上报内容'][i] = '车轮'
    elif pic1['report_name'][i] in ['脚踏损坏','无法踩踏骑行']:
        pic1['上报内容'][i] = '脚踏'
    else:
        pic1['上报内容'][i] = '其他'


# 上报内容分类汇总
pic1_data = pic1.groupby(['上报内容'])['report_cnt'].sum()
label1 = list(pic1_data.index)
data1 = list(pic1_data.values)
plt.rcParams['font.sans-serif']=['Arial Unicode MS']     
plt.rcParams['axes.unicode_minus']=False       
plt.figure(figsize=(5.5,5.5)) 
plt.rcParams['savefig.dpi'] = 120 
plt.rcParams['figure.dpi'] = 120
plt.pie(data1,labels=label1,autopct='%1.2f%%') 
plt.title("全国近一周用户上报占比饼图")
plt.savefig("pic1.jpg")
plt.show()  
# 保存二进制 
import base64
with open("pic1.jpg", 'rb') as f:
    base64_data = base64.b64encode(f.read())
    s = base64_data.decode()
    pic1_decode = 'data:image/jpeg;base64,' + s
'''
'刹车'= ['刹车','刹车故障','刹车失灵']
'车头' = ['车头','车头变形']
'转把' = ['转把故障/破坏','转把']
'前灯' = ['前灯不亮','前灯']
'座椅' = ['座椅','鞍座破坏/故障']
'脚撑' = ['脚撑损坏','脚撑','脚支撑故障']
'链条' = ['链条','链条损坏/脱落']
'挡泥板' = ['挡泥板']
'无动力' = ['电池','骑行无动力','整车无电','骑行中断电']
'开关锁' = ['开关锁','电池锁故障',]
'车轮' = ['车轮']
'其他' = ['人工审核中','其他','其他部件请备注','无法扫码开锁','控制器丢失/破坏',
         '前叉变形','车篮损坏/破损','上私锁']
'脚踏' = ['脚踏损坏','无法踩踏骑行']
'''
# cpo占比图
pic2 = pd.read_csv('pic2.csv',encoding = 'gbk')
for i in range(0,len(pic2)):
    if pic2['name'][i] in ['二维码损坏无法开锁', '骑行中自动关锁', '(恶意)私锁举报']:
        pic2['name'][i] = '其他'
label2 = list(pic2['name'])
data2 = list(pic2['cpo_num'])
pic2_data = pic2.groupby(['name'])['cpo_num'].sum()
label2 = list(pic2_data.index)
data2 = list(pic2_data.values)
plt.figure(figsize=(5.5,5.5)) 
plt.rcParams['savefig.dpi'] = 120 
plt.rcParams['figure.dpi'] = 120
plt.pie(data2,labels=label2,autopct='%1.2f%%') 
plt.title("全国近一周cpo占比饼图")
plt.savefig("pic2.jpg")
plt.show()  
with open("pic2.jpg", 'rb') as f:
    base64_data = base64.b64encode(f.read())
    s = base64_data.decode()
    pic2_decode = 'data:image/jpeg;base64,' + s
# 上报率图
pic3 = pd.read_csv('pic3.csv',encoding = 'gbk')
pic3.set_index(['pt'], inplace=True)
pic3.columns
pic3['passenger_report_ratio'] = pic3['passenger_report_ratio'] * 100
pic3['passenger_brake_report_ratio'] = pic3['passenger_brake_report_ratio'] * 100
pic3['passenger_power_report_ratio'] = pic3['passenger_power_report_ratio'] * 100
plt.figure(figsize=(15,5)) 
plt.rcParams['savefig.dpi'] = 120 #图片像素
plt.rcParams['figure.dpi'] = 120
x=np.arange(len(pic3.index))+1 
y1=np.array(list(pic3['passenger_report_ratio']))
y2=np.array(list(pic3['passenger_brake_report_ratio']))
y3=np.array(list(pic3['passenger_power_report_ratio']))
xticks1=list(pic3.index) 
plt.plot(x,y1,label='用户上报率')
plt.plot(x,y2,label='用户刹车上报率')
plt.plot(x,y3,label='用户无动力上报率')
#设置x轴的刻度
plt.xticks(x,xticks1,size='small',rotation=30)
plt.yticks([])
#设置数字标签
for a,b in zip(x,y1):
    plt.text(a, b, '%.2f' % b + '%', ha='center', va= 'bottom',fontsize=7)
for a,b in zip(x,y2):
    plt.text(a, b, '%.2f' % b + '%', ha='center', va= 'bottom',fontsize=7)
for a,b in zip(x,y3):
    plt.text(a, b, '%.2f' % b + '%', ha='center', va= 'bottom',fontsize=7)
plt.title('全国近1个月上报率走势')
plt.legend(loc='best')
plt.savefig("pic3.jpg")
plt.show()
with open("pic3.jpg", 'rb') as f:
    base64_data = base64.b64encode(f.read())
    s = base64_data.decode()
    pic3_decode = 'data:image/jpeg;base64,' + s
#准确率完成率图
pic4 = pd.read_csv('pic4.csv',encoding = 'gbk')
pic4.set_index(['pt'], inplace=True)
pic4.columns
pic4['passenger_brake_report_cnt'] = pic4['passenger_brake_report_cnt'] * 100
pic4['workorder_precision'] = pic4['workorder_precision'] * 100
pic4['precision'] = pic4['precision'] * 100
plt.figure(figsize=(15,5)) 
plt.rcParams['savefig.dpi'] = 120 
plt.rcParams['figure.dpi'] = 120
x=np.arange(len(pic4.index))+1 
y1=np.array(list(pic4['passenger_brake_report_cnt']))
y2=np.array(list(pic4['workorder_precision']))
y3=np.array(list(pic4['precision']))
xticks1=list(pic4.index) 
plt.plot(x,y1,label='工单完成率')
plt.plot(x,y2,label='工单准确率')
plt.plot(x,y3,label='模型准确率')
#设置x轴的刻度
plt.xticks(x,xticks1,size='small',rotation=30)
plt.yticks([])
plt.title('全国近1个月准确率、完成率走势')
#设置数字标签
for a,b in zip(x,y1):
    plt.text(a, b, '%.1f' % b + '%', ha='center', va= 'bottom',fontsize=7)
for a,b in zip(x,y2):
    plt.text(a, b, '%.1f' % b + '%', ha='center', va= 'bottom',fontsize=7)
for a,b in zip(x,y3):
    plt.text(a, b, '%.1f' % b + '%', ha='center', va= 'bottom',fontsize=7)
plt.legend(loc='best')
plt.savefig("pic4.jpg")
plt.show()
with open("pic4.jpg", 'rb') as f:
     base64_data = base64.b64encode(f.read())
     s = base64_data.decode()
     pic4_decode = 'data:image/jpeg;base64,' + s








content += '<br></br>\n'
content +=  '<img src= \"' + pic1_decode + '\">' 
content +=  '<img src= \"' + pic2_decode + '\">' 
content +=  '<img src= \"' + pic3_decode + '\">' 
content +=  '<img src= \"' + pic4_decode + '\">' 

wf = open('paasenger_report.html', 'w')
wf.write(content)

print (content)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值