前言
图就是门面,一张好图很涨脸!
当然这张图有点花了,广告倒是可以,科研就算了
感觉用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")
)