(3)原神角色数据分析-3

绘图类

在名为"WRITEPHOT.py"的文件中,定义如下绘图方式,则在主页面(app.py)文件中,可通过如下方式调用:

python 复制代码
from WRITEPHOTO import WriteScatter,WriteFunnel,WriteBarData,WritePie,WriteLineBar

代码如下:

python 复制代码
"""绘图"""
from pyecharts.charts import Scatter
from pyecharts import options as opts
from pyecharts.charts import Funnel
from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
from pyecharts.faker import Faker
import numpy as np
import pandas as pd


# 散点图
class WriteScatter:
    Name = None
    Hp = None
    Def = None
    Atk = None

    def __init__(self, Name, Hp, Def, Atk):
        self.Name = Name
        self.Hp = Hp
        self.Def = Def
        self.Atk = Atk

    def write_scatter(self):
        from pyecharts.faker import Faker

        c = (
            Scatter()
            .add_xaxis(list(self.Name))
            .add_yaxis("生命值", list(self.Hp))
            .add_yaxis("防御力", list(self.Def))
            .add_yaxis("攻击力", list(self.Atk))
            .set_global_opts(
                title_opts=opts.TitleOpts(title="Scatter-VisualMap(Size)"),
                visualmap_opts=opts.VisualMapOpts(type_="size", max_=150, min_=20),
                xaxis_opts=opts.AxisOpts(axislabel_opts={"rotate": 45})
            )
            .render("./templates/筛选结果-自动生成.html")
        )


# 漏斗图
class WriteFunnel:
    # 角色名称
    role_name = None
    # 分类依据
    type_things = None

    def __init__(self, role_name, type_things):
        self.role_name = role_name
        self.type_things = type_things

    # zip()函数
    # >>> a = ['a', 'b', 'c', 'd']
    # >>> b = ['1', '2', '3', '4']
    # >>> list(zip(a, b))
    # [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
    def write_funnel(self):
        c = (
            Funnel()
            .add(
                self.type_things,
                [list(z) for z in zip(self.role_name, self.type_things)],
                sort_="ascending",
                label_opts=opts.LabelOpts(position="inside"),
            )
            .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-Sort(ascending)"))
            .render("./templates/排序结果-自动生成.html")
        )


# 数据集
class WriteBarData:
    def __init__(self, df):
        self.df = df

    def write_bar_data(self):
        list_source = [["produce", "生命值", "防御力", "攻击力"]]
        for x in range(0, len(self.df["角色"])):
            list_source.append([self.df.iloc[x]["角色"],
                                int(self.df.iloc[x]["生命值"]),
                                int(self.df.iloc[x]["防御力"]),
                                int(self.df.iloc[x]["攻击力"])])
        print(list_source)
        c = (
            Bar()
            .add_dataset(
                source=list_source
            )
            .add_yaxis(series_name="生命值", y_axis=[])
            .add_yaxis(series_name="防御力", y_axis=[])
            .add_yaxis(series_name="攻击力", y_axis=[])
            .set_global_opts(
                title_opts=opts.TitleOpts(title="标题"),
                xaxis_opts=opts.AxisOpts(type_="category"),
            )
            .render("./templates/数据集-自动生成.html")
        )


# 饼图
class WritePie:
    def __init__(self, three_things, role_type, role_add):
        self.role_things = three_things
        self.role_type = role_type
        self.role_add = role_add

    def write_pie(self):
        print()
        print(self.role_things)
        c = (
            Pie()
            .add(
                "",
                # 这块可以是Series和Series,但不能是Series和list,因为两者无法匹配
                [list(z) for z in zip(["生命值", "防御力", "攻击力"],
                                      [int(self.role_things[0]), int(self.role_things[1]), int(self.role_things[2])])],
                radius=["40%", "75%"],
            )
            .set_global_opts(
                title_opts=opts.TitleOpts(title=f"{self.role_type}属性角色/{self.role_add}"),
                legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),
            )
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
            .render("./templates/单个角色数据-自动生成.html")
        )


# 时间柱状图
class WriteLineBar:
    def __init__(self):
        self.role_name = ["火", "水", "冰", "风", "雷", "岩"]

    def write_lin_bar(self):
        tl = Timeline()
        for i in self.role_name:
            role_things = pd.read_excel("C:/Users/YHT/Desktop/项目/原神各属性角色信息.xlsx", header=0, index_col=0)
            role_things = role_things.fillna(axis=0, method="ffill")
            things_list = role_things.groupby("属性").groups
            print(role_things.loc[things_list[i]]["角色"])
            bar = (
                Bar()
                .add_xaxis(list(role_things.loc[things_list[i]]["角色"]))
                .add_yaxis("生命值", list(role_things.loc[things_list[i]]["生命值"]))
                .add_yaxis("防御力", list(role_things.loc[things_list[i]]["防御力"]))
                .add_yaxis("攻击力", list(role_things.loc[things_list[i]]["攻击力"]))
                .set_global_opts(title_opts=opts.TitleOpts())
            )
            tl.add(bar, f"{i}属性")
            tl.render("./templates/时间柱状图所有角色-自动生成.html")
相关推荐
诸神缄默不语44 分钟前
Python 3中的win32com使用教程+示例:从Excel读取数据生成Word格式报告批量发邮件
python·word·excel
草莓熊Lotso2 小时前
unordered_map/unordered_set 使用指南:差异、性能与场景选择
java·开发语言·c++·人工智能·经验分享·python·网络协议
二川bro7 小时前
量子计算入门:Python量子编程基础
python
夏天的味道٥8 小时前
@JsonIgnore对Date类型不生效
开发语言·python
tsumikistep8 小时前
【前后端】接口文档与导入
前端·后端·python·硬件架构
小白学大数据8 小时前
Python爬虫伪装策略:如何模拟浏览器正常访问JSP站点
java·开发语言·爬虫·python
江上月5139 小时前
Pandas 高级教程:解锁数据分析的强大潜能
数据挖掘·数据分析·pandas
wasp5209 小时前
Apache Hudi 项目总体分析
数据挖掘·apache·hudi·数据湖仓
头发还在的女程序员10 小时前
三天搞定招聘系统!附完整源码
开发语言·python
温轻舟10 小时前
Python自动办公工具06-设置Word文档中表格的格式
开发语言·python·word·自动化工具·温轻舟