Fork me on GitHub

Python_Chart

1、渲染图表

1
2
3
4
5
6
from pyecharts import Bar

bar = Bar("我的第一个图表", "这里是副标题")
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", ], [5, 20, 36, 10, 75])
# bar.print_echarts_options() # 该行只为了打印配置项,方便调试时使用
bar.render() # 生成本地 HTML 文件

解释

  • add()
    主要方法,用于添加图表的数据和设置各种配置项
  • print_echarts_options()
    打印输出图表的所有配置项
  • render()
    默认将会在根目录下生成一个 render.html 的文件,支持 path 参数,设置文件保存位置,如 render(r"e:\my_first_chart.html"),文件用浏览器打开。

    Note

  • 可以按右边的下载按钮将图片下载到本地,如果想要提供更多实用工具按钮,请在 add() 中设置 is_more_utilsTrue

2、使用主题

  • 安装主题插件
    $ pip install echarts-themes-pypkg
  • 设置主题
    bar.use_theme('dark')

Note

echarts 自带 dark 主题, pyecharts 也就自带了 dark。 echarts-themes-pypkg 提供了 vintage, macarons, infographic, shineroma 主题。

3、多次显示图表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from pyecharts import Bar, Line
from pyecharts.engine import create_default_environment

bar = Bar("我的第一个图表", "这里是副标题")
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", ], [5, 20, 36, 10, 75])

line = Line("我的第一个图表", "这里是副标题")
line.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋"], [5, 20, 36, 10, 75])

env = create_default_environment("html")
# 为渲染创建一个默认配置环境
# create_default_environment(filet_ype)
# file_type: 'html', 'svg', 'png', 'jpeg', 'gif' or 'pdf'

env.render_chart_to_file(bar, path='bar.html')
env.render_chart_to_file(line, path='line.html')
喜欢的可以对我打赏了哟~