使用 python 的 Django 库开发简单的数据可视化网站(IV) - 使用 pyecharts 实现数据可视化
最编程
2024-05-08 20:39:36
...
上节课我们使用了Django连接了MySQL进行了数据的显示和数据的查询,这节课我们使用pyecharts进行数据可视化,由于之前已经讲了一期pyecharts的数据可视化,所以我们这节课会稍微简单一点。
本次开发工具:pycharm和python3.6
本次使用库:pyecharts 安装方式 pip install pyecharts
(一)导包
from pyecharts.charts import Page,Bar,Grid,Pie,Gauge
from pyecharts import options as opts
from pyecharts.globals import ThemeType
import pandas as pd
from bs4 import BeautifulSoup
(二)绘图
由于之前讲过一次pyecharts的文章,所以这次我直接放源代码了
柱状图
柱状图有两个,这边只放一个的代码,其他类似
def get_grid_1():
#name = ['gonggong','jiguan','jisuanji','ruanjian','shiyan','tongxin','wangluoanquan','yuanlingdao']
name = ['公共课部','机关','计算机科学','软件工程系','实验中心','通信工程系','网络空间安全','院领导']
data = [83,65,281,353,299,87,86,85]
bar1 = (Bar().add_xaxis(name).add_yaxis("文献量",data,yaxis_index=0,gap='10%').extend_axis(
yaxis=opts.AxisOpts(name='文献数量'),)).set_global_opts(title_opts=opts.TitleOpts(title="各学院发表文章情况",pos_left = 'left',pos_top = '0px'),xaxis_opts=opts.AxisOpts(name_rotate=60,name="院系名称",axislabel_opts={"rotate":45}))\
#.render('first.html')
return bar1
玫瑰图
玫瑰图有三个,这边只放一个图的代码,其他类似
def get_grid_2():
names = ['张宪立', '李聪', '王连相', '邹晓', '周小健', '朱爱红', '廖成斌', '滕永晨', '张墨逸', '王春霞', '张霞', '曲培娟', '吴敏', '侯亮', '王兰勇', '谢莉',
'金亚蓉', '毛开富', '张娟', '徐志刚', '赵宏', '姚毓凯', '王燕', '李睿', '王旭阳', '任学惠', '庞淑侠', '刘嘉', '刘鹏', '朱红蕾', '段宏湘', '纪东升',
'丁凡', '张红', '顾群', '施秋霞', '高玮军', '唐建新', '张秋余', '张其文', '张聚礼', '柯铭', '任旭鹏', '陈作汉', '陈昊', '孙为', '杜红林', '王志祥',
'姚斌', '康兴民', '王森', '陈伟', '张娜', '何继爱', '贾科军', '彭铎', '王惠琴', '陈海燕', '王维芳', '郑玉峰', '曹明华', '蔺莹', '李立', '彭清斌',
'朱宁宁', '许亚美', '王婵飞', '郭显', '董瑞洪', '曹来成', '晏燕', '方君丽', '王晶', '张迪', '马维俊', '谢鹏寿', '包仲贤', '贵向泉', '张恩展', '王璐',
'杜谨泽', '李英堂', '冯涛', '曾贤虎', '年福忠', '张玺君', '陈育苗', '李晓旭']
articlenum = [6, 1, 1, 6, 5, 6, 9, 9, 14, 6, 20, 8, 6, 12, 1, 4, 1, 7, 26, 17, 46, 1, 63, 27, 27, 5, 6, 5, 26, 15,
7, 2, 7, 27, 9, 1, 17, 7, 190, 21, 5, 19, 9, 14, 7, 19, 2, 1, 3, 10, 2, 4, 13, 51, 26, 19, 58, 30, 4,
6, 53, 14, 7, 7, 10, 4, 10, 15, 17, 18, 25, 6, 5, 1, 5, 30, 14, 10, 6, 14, 1, 6, 39, 7, 8, 19, 1, 11]
df = pd.DataFrame() # 生成一个空DataFrame()
df['教师姓名'] = names
df['发表文章数量'] = articlenum
df.sort_values(by='发表文章数量', ascending=False, inplace=True) # 按销售额从小到大排序
data_pair = [] # 生成画图需要的数据格式
for i,j in zip(df['教师姓名'],df['发表文章数量']):
list_ = [i,j]
data_pair.append(list_) # 画图
pie1 = (Pie().add(series_name="教师",data_pair=data_pair,rosetype="radius", #类型为南丁格尔玫瑰图
radius=["10%","60%"],center=["center", "center"], #设置圆心位置
).set_global_opts(title_opts=opts.TitleOpts(title="教师发表文章数量",pos_left="center",pos_top="0%",title_textstyle_opts=opts.TextStyleOpts(color="pink",font_size = 20,),),
legend_opts=opts.LegendOpts(is_show = False,),).set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}篇"),tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a}{b}: {c}篇 ({d}%)"),))
grid1 = (Grid(init_opts=opts.InitOpts(theme = ThemeType.WONDERLAND #设置主题
)).add(pie1, grid_opts=opts.GridOpts(pos_left='0%',)))\
#.render('second.html')
return grid1
导航图
def get_grid_6():
gague = (Gauge(init_opts=opts.InitOpts(width="400px", height="200px"))
.add(series_name='文章数',data_pair=[['',1339]])).set_global_opts(
title_opts=opts.TitleOpts(title="学院发表文章总数量",pos_left="center",pos_top="0%",title_textstyle_opts=opts.TextStyleOpts(color="pink",font_size = 20,),),
legend_opts=opts.LegendOpts(is_show=False),tooltip_opts=opts.TooltipOpts(is_show=True,))\
#.render('sixth.html')
return gague
添加标题
def tab1(name,color): #作为标题吧
c = (Pie().
set_global_opts(
title_opts=opts.TitleOpts(title=name,pos_left='center',pos_top='center',
title_textstyle_opts=opts.TextStyleOpts(color=color,font_size=35))))
return c
将所有图汇总
def get_page():
a1 = get_grid_1()
a2 = get_grid_2()
a3 = get_grid_3()
a4 = get_grid_4()
a5 = get_grid_5()
a6 = get_grid_6()
a7 = tab1("计算机学院文献数据可视化","#FF00FF")
page = (Page().add(a7).add(a6).add(a5).add(a4).add(a3).add(a2).add(a1))
return page
(三)改变网页格式
with open("zuizhong.html", "r+", encoding='utf-8') as html:
html_bf = BeautifulSoup(html, 'lxml')
divs = html_bf.select('.chart-container')
divs[0]["style"] = "width:40%;height:10%;position:absolute;top:0;left:30%;"
divs[1]["style"] = "width:40%;height:40%;position:absolute;top:10%;left:30%;"
divs[2]["style"] = "width:40%;height:50%;position:absolute;top:50%;left:30%;"
divs[3]["style"] = "width:30%;height:50%;position:absolute;top:0;left:0;"
divs[4]["style"] = "width:30%;height:50%;position:absolute;top:50%;left:0;"
divs[5]["style"] = "width:30%;height:50%;position:absolute;top:0;left:70%;"
divs[6]['style'] = "width:30%;height:50%;position:absolute;top:50%;left:70%;"
body = html_bf.find("body")
body["style"] = "background-color:#07645D;" # 设置网页背景颜色
#div_title = "兰州理工大学计算机学院文献数据可视化"
#body.insert(0, BeautifulSoup(div_title, "lxml").div)
html_new = str(html_bf)
html.seek(0, 0)
html.truncate()
html.write(html_new)
html.close()
(4)
将最后完成的网页放到Django的templates模板文件夹中
总结:
这就是这次Django开发网站的所有过程。大家有什么问题可以直接在后台私聊我呢。
上一篇: 2023.10.8 寒露(一面见地)
推荐阅读
-
使用 python 的 Django 库开发简单的数据可视化网站(IV) - 使用 pyecharts 实现数据可视化
-
35 岁实现财务*,腾讯程序员手握2300万提前退休?-1000万房产、1000万腾讯股票、加上300万的现金,一共2300万的财产。有网友算了一笔账,假设1000万的房产用于自住,剩下1300万资产按照平均税后20-50万不等进行计算,大约花上26-60年左右的时间才能赚到这笔钱。也就是说,普通人可能奋斗一辈子,才能赚到这笔钱。在很多人还在为中年危机而惶惶不可终日的时候,有的人的35岁,就已经安全着陆,试问哪个打工人不羡慕?但问题是有这样财富积累必然有像样的实力做靠山。没有人可以不劳而获。 看到这里,肯定有人说,那么对于普通人来说,卷可能真就成了唯一的出路。但是卷也有轻松的卷,“偷懒”的卷法,对于程序员而言,刨除掉一时无法改掉的开会传统占用的大部分时间,如何把有限的时间和精力放在真正重要的架构设计、需求设计上,而不是重复的造*,编码、改bug、手动测试。因此在科技改变生活的今天,学会使用AI工具成为程序员们的必备技能。 以全栈式全自动的软件开发工具飞算SoFlu软件机器人为例,作为全球首款面向微服务架构设计和最佳实践的软件机器人,SoFlu软件机器人改变了原来手工编码的作业模式,通过可视化拖拽方式以及参数配置就能实现等同于编写复杂代码的业务逻辑,在设计业务逻辑时就完成了微服务应用开发,做到“业务即图,图即代码”,实现“软件开发,十倍提效”。 而作为飞算SoFlu软件机器人的一个重要组成部分,FuncGPT(慧函数)支持所有类型函数创建。通过自然语言描述Java函数需求,实时生成高质量、高可读性的Java函数代码。生成代码可直接复制到IDEA,或一键导入Java全自动开发工具函数库。 FuncGPT(慧函数)具有五大核心能力: ● 自然语言:通过自然语言即可生成函数,降低软件开发门槛。 ● 秒级函数开发:全自动秒级开发函数,秒级完成,效率千倍提升。 ● 一站式函数开发应用:函数生成即可直接应用,效率、安全有保障。 ● 质量可靠:生成的函数代码符合业界规范、具备良好的可读性,符合最优解。 ● 灵活生成:可以根据开发人员的具体需求生成并支持调整和修改。 以“将文件流转换为File对象”这个需求为例,以下是FuncGPT(慧函数)秒级生成的代码: / 类名:FileConverter // 函数名:convertStreamToFile // 函数功能:将文件流转换为File对象 // POM依赖包:无 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class FileConverter { /** * 将文件流转换为File对象 * @param inputStream 文件流 * @param filePath 文件路径 * @param fileName 文件名 * @return 转换后的File对象 * @throws IOException 如果发生I/O异常 */ public static File convertStreamToFile(InputStream inputStream, String filePath, String fileName) throws IOException { File file = new File(filePath + File.separator + fileName); // 创建File对象 try (FileOutputStream outputStream = new FileOutputStream(file)) { // 创建文件输出流 byte buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { // 从文件流读取数据并写入文件 outputStream.write(buffer, 0, bytesRead); } } return file; // 返回转换后的File对象 } } // 函数示例 // 将文件流转换为File对象示例 // 入参:inputStream,文件流 // 入参:filePath,文件路径 // 入参:fileName,文件名 // 出参:file,转换后的File对象 // 调用示例: // InputStream inputStream = new FileInputStream("example.txt"); // String filePath = "C:\\Users\\User\\Documents"; // String fileName = "example.txt"; // File file = FileConverter.convertStreamToFile(inputStream, filePath, fileName); // System.out.println(file.getAbsolutePath); // 输出结果:例如,将文件流转换为File对象后,文件的绝对路径为:C:\Users\User\Documents\example.txt // 则输出结果为:C:\Users\User\Documents\example.txt 通过分析,不难发现以上代码:
-
使用Python的pyecharts库实现数据可视化