其他分享
首页 > 其他分享> > pyecharts可视化之前戏

pyecharts可视化之前戏

作者:互联网

 

 

官方文档:简介 - pyecharts - A Python Echarts Plotting Library built with love.

 

安装的话比较简单,自己去安装

 

查看自己的版本:

  import pyecharts

  print(pyecharts.__version__)

 

一、 创建方式(作条形图例子)

1、方式一

 

bar = Bar()

# 添加x轴(横轴)的一系列名字,类型能有以下
bar.add_xaxis(['汉字',2,True,4,5,6])

# 添加y轴的名字,和对应的值,默认为0
bar.add_yaxis("x轴名字对应的值", [8, 10, 57, 10, 75,55])

# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
bar.render()

运行效果:

 

 

 

 

2、pyecharts 所有方法均支持链式调用。

# 从该模块导入柱状图
from pyecharts.charts import Bar


bar = (
    Bar()
    .add_xaxis(['汉字',2,True,4,5,6])
    .add_yaxis("x轴名字对应的值", [8, 10, 57, 10, 75,55])
)
bar.render('test.html')

运行效果与上面一致:

 

 

 

3、使用 options 配置项,在 pyecharts 中,一切皆 Options。

# 导入条形图
from pyecharts.charts import Bar

# 导入配置,并作为。。
from pyecharts import options as opts

# V1 版本开始支持链式调用
# 你所看到的格式其实是 `black` 格式化以后的效果
# 可以执行 `pip install black` 下载使用
bar = (
    Bar()
    .add_xaxis(['汉字',2,True,4,5,6])
    .add_yaxis("x轴名字对应的值", [8, 10, 57, 10, 75,55])
    
    #                       配置
    .set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
    # 或者直接使用字典参数
    # .set_global_opts(title_opts={"text": "主标题", "subtext": "副标题"})
)
bar.render()

# 不习惯链式调用的开发者依旧可以单独调用方法
bar = Bar()
bar.add_xaxis(['汉字',2,True,4,5,6])
bar.add_yaxis("x轴名字对应的值", [8, 10, 57, 10, 75,55])
bar.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
bar.render()

运行效果:

 

 

 

二、渲染成图片文件,这部分内容请参考 进阶话题-渲染图片

from pyecharts.charts import Bar
from pyecharts.render import make_snapshot

# 使用 snapshot-selenium 渲染图片
from snapshot_selenium import snapshot

bar = (
    Bar()
    .add_xaxis(['汉字',2,True,4,5,6])
    .add_yaxis("x轴名字对应的值", [8, 10, 57, 10, 75,55])
)
make_snapshot(snapshot, bar.render(), "bar.png")

运行有误,没调成功。

 

 

 

三、使用主题:

 pyecharts 提供了 10+ 种内置主题,开发者也可以定制自己喜欢的主题,进阶话题-定制主题 有相关介绍。

 

from pyecharts.charts import Bar
from pyecharts import options as opts
# 内置主题类型可查看 pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType

bar = (
    # 会变色,不一样
    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
    .add_xaxis(['汉字',2,True,4,5,6])
    .add_yaxis("x轴名字对应的值", [8, 10, 57, 10, 75,55])
    .add_yaxis("另外一个x轴名字对应的值", [8, 10, 57, 10, 75,55])
    .set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
)

bar.render()

运行效果:

 

  Note: 在使用 Pandas&Numpy 时,请确保将数值类型转换为 python 原生的 int/float。比如整数类型请确保为 int,而不是 numpy.int32

 

四、使用Notebook

当然你也可以采用更加酷炫的方式,使用 Notebook 来展示图表,matplotlib 有的,pyecharts 也会有的。pyecharts 支持 Jupyter Notebook / Jupyter Lab / Nteract / Zeppelin 四种环境的渲染。

 

标签:10,pyecharts,之前,Bar,add,可视化,bar,opts
来源: https://www.cnblogs.com/yue-ge6/p/15716622.html