Github/GItee仓库地址

注意

  • 数据传入到 pyecharts 的时候,需要自行将数据格式转换成上述 Python 原生的数据格式。
  • 方法:
# for int
[int(x) for x in your_numpy_array_or_something_else]
# for float
[float(x) for x in your_numpy_array_or_something_else]
# for str
[str(x) for x in your_numpy_array_or_something_else]
  • 便捷方法: pandas.Series.tolist()

生成数据

import numpy as np
# 生成数据
x_data = [chr(i) for i in range(65, 91)]  # lable,26个字母
[str(i) for i in x_data]
y_data = np.random.uniform(5,200,26) # data
[float(i) for i in y_data]
# 将数据转换为列表加元组的格式([(key1, value1), (key2, value2)])
data=[list(z) for z in zip(x_data, y_data)]

乱序数据

  • 南丁格尔图,通过半径区分数据大小,有’radius’和’area’两种模式。
    • radius:扇区圆心角展现数据的百分比,半径展现数据的大小
    • area:所有扇区圆心角相同,仅通过半径展现数据大小
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType

# 创建饼形图并初始化配置项
fig = Pie(init_opts=opts.InitOpts(
    width='1200px', height='600px', theme=ThemeType.ESSOS, bg_color='white'))

# 为饼形图添加数据
fig.add(
    series_name="States",  # 系列名称
    data_pair=data,  # 系列数据, 格式为 [(key1, value1), (key2, value2)]
    radius=["15%", "80%"],  #内外半径
    center=["25%", "50%"],  # 位置 1
    rosetype='radius',  # 按圆心角区分
    color='auto')

fig.add(
    series_name="States",  # 系列名称
    data_pair=data,  # 系列数据, 格式为 [(key1, value1), (key2, value2)]
    radius=["15%", "80%"],  #内外半径
    center=["75%", "50%"],  # 位置 2
    rosetype='area',  # 按半径展示
    color='auto')

# 设置全局配置项
fig.set_global_opts(
    title_opts=opts.TitleOpts(title="两种南丁格尔图"),
    legend_opts=opts.LegendOpts(is_show=False),  # 不显示Legend图例
    visualmap_opts=opts.VisualMapOpts  # 视觉映射
    (
        is_show=False,  # 不显示映射配置
        type_='size',  # 映射过渡类型,选择size,不选择无法设置颜色
    ))

# 设置系列配置项
fig.set_series_opts(label_opts=opts.LabelOpts(
    is_show=True,  # 显示标签
    position='left',  # 标签位置
    font_style='oblique',  # 字体风格
    font_weight='bold',  # 字体粗细
    font_family='Arial',  # 字体系列
    margin=15,
    font_size=11)  # 字体大小
                    )
# 渲染图片至html
# fig.render('rosetype.html')
# Jupyternotebook渲染图片
fig.render_notebook()

在这里插入图片描述

渲染图片为png或jpeg

  • snapshot-selenium 是 pyecharts + selenium 渲染图片的扩展
  • 使用 selenium 需要配置 browser driver,这部分可以参考 selenium-python 相关介绍
  • 推荐使用 Chrome 浏览器,可以开启 headless 模式。目前支持 Chrome, Safari。
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot

# 使用chrome
make_snapshot(engine=snapshot,file_name=fig.render('rosetype.html'),output_name='C:\Git Code\ScienceGallery\Picture\Rosetype.png',is_remove_html=True)

顺序数据

# 数据排序
data.sort(key=lambda x: x[1])
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType

# 创建饼形图并初始化配置项
fig = Pie(init_opts=opts.InitOpts(
    width='1200px', height='600px', theme=ThemeType.ESSOS, bg_color='white'))

# 为饼形图添加数据
fig.add(
    series_name="States",  # 系列名称
    data_pair=data,  # 系列数据, 格式为 [(key1, value1), (key2, value2)]
    radius=["15%", "80%"],  #内外半径
    center=["25%", "50%"],  # 位置 1
    rosetype='radius',  # 按圆心角区分
    is_clockwise=False,  # 饼图的扇区是否是顺时针排布
    color='auto')

fig.add(
    series_name="States",  # 系列名称
    data_pair=data,  # 系列数据, 格式为 [(key1, value1), (key2, value2)]
    radius=["15%", "80%"],  #内外半径
    center=["75%", "50%"],  # 位置 2
    rosetype='area',  # 按半径展示
    is_clockwise=False,  # 饼图的扇区是否是顺时针排布
    color='auto')

# 设置全局配置项
fig.set_global_opts(
    title_opts=opts.TitleOpts(title="两种南丁格尔图"),
    legend_opts=opts.LegendOpts(is_show=False),  # 不显示Legend图例
    visualmap_opts=opts.VisualMapOpts  # 视觉映射
    (
        is_show=False,  # 不显示映射配置
        type_='size',  # 映射过渡类型,选择size,不选择无法设置颜色
    ))

# 设置系列配置项
fig.set_series_opts(label_opts=opts.LabelOpts(
    is_show=True,  # 显示标签
    position='left',  # 标签位置
    font_style='oblique',  # 字体风格
    font_weight='bold',  # 字体粗细
    font_family='Arial',  # 字体系列
    margin=15,
    font_size=11)  # 字体大小
                    )
# 渲染图片至html
# fig.render('rosetype.html')
# Jupyternotebook渲染图片
fig.render_notebook()

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐