利用pyecharts读取csv并进行数据统计可视化的实现
作者:Haor爱打杂 时间:2023-07-15 07:18:03
因为需要一个html形式的数据统计界面,所以做了一个基于pyecharts包的可视化程序,当然matplotlib还是常用的数据可视化包,只不过各有优劣;基本功能概述就是读取csv文件数据,对每列进行数据统计并可视化,最后形成html动态界面,选择pyecharts的最主要原因就是这个动态界面简直非常炫酷。
先上成品图:
数据读取和数据分析模块:
#导入csv模块
import csv
#导入可视化模块
from matplotlib import pyplot as plt
from pylab import mpl
import numpy as np
import random
from pyecharts import Line,Pie,Grid,Bar,WordCloud
#指定文件名,然后使用 with open() as 打开
python_file = 'haiyang.csv'
#filename = 'release/111.csv'
#python3 LieCharts.py test_chart --python_file 'haiyang.csv'
with open(python_file) as f:
#创建一个阅读器:将f传给csv.reader
reader = csv.reader(f)
#使用csv的next函数,将reader传给next,将返回文件的下一行
header_row = next(reader)
for index, column_header in enumerate(header_row):
print(index, column_header)
#读取置信度
#创建置信度的列表
confidences =[]
#创建风险等级数组
highRisk = []
middleRisk = []
lowRisk = []
noRisk = []
person = []
#创建时间点
timePoint = []
#文件信息
fileInformation = []
#遍历reader的余下的所有行(next读取了第一行,reader每次读取后将返回下一行)
for row in reader:
# 下面就是对某一列数据进行遍历,因为项目保密,就不列出具体代码了,其实就是各种循环语句,大家根据自己的数据简单写一下就行
fileInformation.append('某某某某')
fileInformation.append(row[0])
fileInformation.append(row[1])
fileInformation.append(row[2])
fileInformation.append(len(confidences))
int_confidences = []
for i in confidences:
# 同上
len_noRisk = len(noRisk)
len_lowRisk = len(lowRisk)
len_middleRisk = len(middleRisk)
len_highRisk = len(highRisk)
len_person = len(person)
total = int(len_person+len_highRisk+len_middleRisk+len_lowRisk+len_noRisk)
if (len_highRisk > total/2):
# 同上
数据可视化模块:
pie_title = Pie('某某某分析报表', "", title_pos='center',title_top="1%",title_text_size=42,subtitle_text_size=20)
value=[10000,6181,4386,4055,4000]
wordcloud=WordCloud(width=30,height=12,title="某某某某信息",title_pos="22%",title_top="12%",title_text_size=32)
wordcloud1=WordCloud(width=30,height=12,title="某某:"+fileInformation[1],title_pos="22%",title_top="22%",title_text_size=26)
wordcloud2=WordCloud(width=30,height=12,title="某某:"+fileInformation[2],title_pos="22%",title_top="30%",title_text_size=26)
#wordcloud3=WordCloud(width=30,height=12,title="音频采样率:"+fileInformation[3],title_pos="22%",title_top="38%",title_text_size=26)
#wordcloud4=WordCloud(width=30,height=12,title="总时长/s:"+fileInformation[4],title_pos="22%",title_top="36%",title_text_size=32)
# wordcloud.add("",fileInformation,value,word_size_range=[20,100],rotate_step=3
# ,xaxis_pos=200,grid_left="1%",grid_bottom="50%",grid_top="5%",grid_right="80%")
#折线图
line=Line("某某某某某走势图",title_pos='center',title_top="51%",title_text_size=32,width=600,height = 20)
attr=timePoint
line.add("某某某某某",attr,int_confidences,legend_pos="85%",legend_top="54%",
mark_point=["max","min"],mark_line=["average"])
#饼图
attr=["某某某某", "某某某某", "某某某某", "某某某"]
v1=[len_highRisk, len_middleRisk, len_lowRisk,len_noRisk]
pie=Pie("某某某某某某某",title_pos="65%",title_top="12%",title_text_size=32,width=100,height = 100)
pie.add("",attr,v1,radius=[0,30],center=[71,35],
legend_pos="85%",legend_top="20%" ,legend_orient="vertical")
grid=Grid(width = 1800 ,height= 900)#调整画布大小
grid.add(line,grid_left="5%",grid_bottom="2%",grid_top="60%")
grid.add(pie_title,grid_bottom="10%")
grid.add(wordcloud,grid_left="1%",grid_bottom="50%",grid_top="5%",grid_right="80%")
grid.add(wordcloud1,grid_left="1%",grid_bottom="50%",grid_top="5%",grid_right="80%")
grid.add(wordcloud2,grid_left="1%",grid_bottom="50%",grid_top="5%",grid_right="80%")
#grid.add(wordcloud3,grid_left="1%",grid_bottom="50%",grid_top="5%",grid_right="80%")
#grid.add(wordcloud4,grid_left="1%",grid_bottom="50%",grid_top="5%",grid_right="80%")
grid.add(pie,grid_left="50%",grid_bottom="50%")
#grid.render()
grid.render(path='./release/XXXX.html')
根据需求这个还可以跨平台跨语言调用,比如C++程序调用python进行数据分析。
来源:https://juejin.im/post/5e97e5b66fb9a03c5c5a98e4
标签:pyecharts,读取,csv
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解python的循环
2022-12-29 04:22:52
![](https://img.aspxhome.com/file/2023/3/93813_0s.png)
Label Propagation算法原理示例解析
2022-02-03 22:44:43
![](https://img.aspxhome.com/file/2023/2/131542_0s.jpg)
python实现ip代理池功能示例
2023-07-30 16:36:06
索引的原理及索引建立的注意事项
2012-08-21 10:27:47
Python实现比较两个文件夹中代码变化的方法
2022-04-07 23:16:08
golang构建工具Makefile使用详解
2024-04-28 10:46:04
使用python实现离散时间傅里叶变换的方法
2021-10-26 19:44:07
![](https://img.aspxhome.com/file/2023/1/66451_0s.png)
python 判断是否为正小数和正整数的实例
2022-06-12 01:26:04
python3.8+django2+celery5.2.7环境准备(python测试开发django)
2022-08-19 06:17:14
两个table之间相互移动数据
2024-04-10 13:50:56
Python读取excel中的图片完美解决方法
2022-06-07 09:57:18
![](https://img.aspxhome.com/file/2023/1/87801_0s.png)
Go高级特性探究之稳定排序详解
2023-07-17 16:11:48
mysql实现合并同一ID对应多条数据的方法
2024-01-15 16:19:34
如何决定是否将登录内容保存到Cookie里?
2009-12-16 18:54:00
详解python 内存优化
2022-06-04 16:31:43
Python OpenCV实现姿态识别的详细代码
2023-05-27 23:42:31
![](https://img.aspxhome.com/file/2023/9/78719_0s.jpg)
layerUI下的绑定事件实例代码
2024-04-16 09:38:08
python多线程socket编程之多客户端接入
2021-01-26 10:14:52
![](https://img.aspxhome.com/file/2023/4/69054_0s.png)
Python中的jquery PyQuery库使用小结
2023-05-27 11:08:15
Django模板标签中url使用详解(url跳转到指定页面)
2023-12-18 00:09:54