【PyEcharts】旭日图统计数据集实例:grid组合图表、JS函数渲染

前言

图就是门面,一张好图很涨脸!
在这里插入图片描述
当然这张图有点花了,广告倒是可以,科研就算了
在这里插入图片描述
感觉用PyEcharts的好处在于,可以展开与收起

实现

官方地址:https://pyecharts.org/#/zh-cn/

代码我就不细说了,讲下值得注意的地方
1.将RGB三数值转成16进制,用rgb函数没整明白(找不到),就自个转了下

def RGB_to_Hex(tmp):
    rgb = tmp#将RGB格式划分开来
    strs = '#'
    for i in rgb:
        num = int(i)#将str转int
        #将R、G、B分别转化为16进制拼接转换并大写
        strs += str(hex(num))[-2:].replace('x','0').upper()
    return strs

2.在pie组件说明中,发现这么长的类型支持啥意思啊

data_pair: types.Sequence[types.Union[types.Sequence, opts.PieItem, dict]],

在这里插入图片描述3.有些变量时可以支持JSFunc的,譬如Sunburst add函数中的sort_
在这里插入图片描述但是吧,它的null似乎不起作用,咱用个lamda函数实现方可

sort_=JsCode("""a=>{a}"""), # 按照原输出,不排序

JS函数的作用呢就是,在渲染是拿到该数据时的处理方式
另一个示例:
方式2:

fn = """
    function(params) {
        if(params.name == '其他')
            return '\\n\\n\\n' + params.name + ' : ' + params.value + '%';
        return params.name + ' : ' + params.value + '%';
    }
    """
def new_label_opts():
    return opts.LabelOpts(formatter=JsCode(fn), position="center")
c = (
    Pie()
    .add(
        "",
        [list(z) for z in zip(["剧情", "其他"], [25, 75])],
        center=["20%", "30%"],
        radius=[60, 80],
        label_opts=new_label_opts(),
    )

方式3
在这里插入图片描述

详细代码

from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
import json
"""
Gallery 使用 pyecharts 1.2.1
参考地址: https://www.echartsjs.com/examples/editor.html?c=sunburst-simple

目前无法实现的功能:

1、暂无
"""
def RGB_to_Hex(tmp):
    rgb = tmp#将RGB格式划分开来
    strs = '#'
    for i in rgb:
        num = int(i)#将str转int
        #将R、G、B分别转化为16进制拼接转换并大写
        strs += str(hex(num))[-2:].replace('x','0').upper()
    return strs

with open("stat_classify_COD10K.json.json") as f:
    data_dict = json.load(f)
# 保存为csv文件
sub_classes = data_dict["classes"]
sub_cnt_list = data_dict["cnt_list"]
sub_colors = data_dict["colors"]

super_index_list = [range(20),range(20,49),range(49,66),range(66,68),range(68,69)]    # 大类下子类索引分布
super_classes = ["Aquatic","Terrestrial","Flying","Amphibian","Other"]

data = []
for i,item in enumerate(super_index_list):
    _dict = {"name":super_classes[i],"itemStyle": {"color": "#da0d68"},"children":[]}
    for sub_class_index in item:
        sub_dict = {
                "name": sub_classes[sub_class_index],
                # "itemStyle": {"color": RGB_to_Hex(sub_colors[sub_class_index])},
                "itemStyle": {"color": "black"},
                "children": [
                    {
                        "name": str(sub_cnt_list[sub_class_index]),
                        "value": 1,
                        "itemStyle": {"color": RGB_to_Hex(sub_colors[sub_class_index])},
                    },
                ],
            }
        _dict["children"].append(sub_dict)
    data.append(_dict)

sun = (
    Sunburst(init_opts=opts.InitOpts(width="1000px", height="600px"))
    .add(
        "COD10K",
        data_pair=data,
        highlight_policy="ancestor",
        radius=[0, "95%"],
        sort_=JsCode("""a=>{a}"""), # 按照原输出,不排序
        levels=[
            {},
            {
                "r0": "15%",
                "r": "25%",
                "itemStyle": {"borderWidth": 2},
                "label": {"rotate": "tangential"},
            },
            {"r0": "25%", "r": "70%", "label": {"align": "right"}},
            {
                "r0": "70%",
                "r": "72%",
                "label": {"position": "outside", "padding": 3, "silent": False},
                "itemStyle": {"borderWidth": 3},
            },
        ],
    )
)

pie = (
    Pie()
    .add(
        "",
        data_pair=[opts.PieItem(name=item[0],value=item[1],
                itemstyle_opts={"color": RGB_to_Hex(item[2])}
         ) for item in zip(sub_classes, sub_cnt_list,sub_colors)],   # types.Sequence[types.Union[types.Sequence, opts.PieItem, dict]], 注union标志,选择其中一个方式就好
        radius=["25%", "70%"],
        center=["50%", "50%"],
        rosetype="area",
        label_opts=opts.LabelOpts(is_show=False),
    )
    .set_global_opts(legend_opts=opts.LegendOpts(is_show=False))
)
    
grid = (
    Grid()
    .add(sun,grid_opts=opts.GridOpts(pos_top="50%", pos_right="50%"))
    .add(pie,grid_opts=opts.GridOpts(pos_top="50%", pos_right="50%"))
    .render("sunburst_flavors.html")
)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空•物语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值