前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用python的Django库开发一个简单的数据可视化网站(四)- 使用pyecharts进行数据可视化

使用python的Django库开发一个简单的数据可视化网站(四)- 使用pyecharts进行数据可视化

作者头像
你像时光唯美i
发布2022-08-26 14:05:46
1.3K0
发布2022-08-26 14:05:46
举报
文章被收录于专栏:python开发教程

上节课我们使用了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的文章,所以这次我直接放源代码了

柱状图

柱状图有两个,这边只放一个的代码,其他类似

代码语言:javascript
复制
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

玫瑰图

玫瑰图有三个,这边只放一个图的代码,其他类似

代码语言:javascript
复制
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

导航图

代码语言:javascript
复制
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

添加标题

代码语言:javascript
复制
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

将所有图汇总

代码语言:javascript
复制
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

(三)改变网页格式

代码语言:javascript
复制
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开发网站的所有过程。大家有什么问题可以直接在后台私聊我呢。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-05-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 python开发教程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档