pyecharts说明文档
https://pyecharts.org/#/zh-cn/global_options?id=legendopts%ef%bc%9a%e5%9b%be%e4%be%8b%e9%85%8d%e7%bd%ae%e9%a1%b9
pyecharts安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts
pyecharts快速入门
pyecharts中可以绘制的图有很多,这里我们先来总体的了解一下他的使用风格,和调用的方式。
pyecharts 分为 v0.5.X 和 v1 两个大版本,v0.5.X 和 v1 间不兼容,v1 是一个全新的版本.经开发团队决定,0.5.x 版本将不再进行维护,0.5.x 版本代码位于 05x 分支
import pyecharts
pyecharts.__version__
'1.9.0'
pyecharts简单使用
from pyecharts.charts import Bar
实例化
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A",[5,20,36,10,75,90])
render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
也可以传入路径参数,如 bar.render("mycharts.html")
bar.render()
# 实例化
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A",[5,20,36,10,75,90])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
bar.render()
设置在notebook上展示
bar.render_notebook()
# 实例化
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A",[5,20,36,10,75,90])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
bar.render_notebook()
运行效果:
链式调用
对上述代码进行重整,得到链式调用。
from pyecharts.charts import Bar
bar = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A",[5,20,36,10,75,90])
)
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
bar.render_notebook()
运行效果同上图
保存图片
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
# 使用 snapshot-selenium 渲染图片
from snapshot_selenium import snapshot
bar = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A",[5,20,36,10,75,90])
.add_yaxis("商家B",[10,30,46,60,35,10])
)
make_snapshot(snapshot,bar.render(),"bar.png")
全局配置
全局配置项
我们来看下全局配置项有哪些。在学习具体的配置项之前,先来看下pyecharts生成的图由哪几个部分组成。
针对以上每个部分,都有相应的配置项来进行配置。所有的配置类,都是放到pyecharts.options中。
-# 06-2配置项
![image](https://upload-images.jianshu.io/upload_images/25981608-76f72fb75161164e.jpeg?imageMogr2/auto
- 初始化配置项
可以配置诸如图像宽度,高度,图表主题,背景颜色等。
class pyecharts.options.InitOpts
class InitOpts(
# 图表画布宽度,css 长度单位。
width: str = "900px",
# 图表画布高度,css 长度单位。
height: str = "500px",
# 图表 ID,图表唯一标识,用于在多图表时区分。
chart_id: Optional[str] = None,
# 渲染风格,可选 "canvas", "svg"
# # 参考 `全局变量` 章节
renderer: str = RenderType.CANVAS,
# 网页标题
page_title: str = "Awesome-pyecharts",
# 图表主题
theme: str = "white",
# 图表背景颜色
bg_color: Optional[str] = None,
# 远程 js host,如不设置默认为 https://assets.pyecharts.org/assets/"
# 参考 `全局变量` 章节
js_host: str = "",
# 画图动画初始化配置,参考 `global_options.AnimationOpts`
animation_opts: Union[AnimationOpts, dict] = AnimationOpts(),
)
根椐上述教程,初始化配置项是调用pyecharts下options的类方法InitOpts所以from pyecharts import options as opts、
Bar(init_opts=opts.InitOpts(width= "900px",height= "500px",page_title= "abc",theme= ThemeType.CHALK,bg_color = 'black'))
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
bar = (
Bar(init_opts=opts.InitOpts(width= "900px",
height= "500px",page_title= "abc",
theme= ThemeType.CHALK,bg_color = 'black'))
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A",[5,20,36,10,75,90])
)
bar.render_notebook()
- 标题配置项
class pyecharts.options.TitleOpts
.set_global_opts()
.set_global_opts(title_opts = opts.TitleOpts(title='销售对比',title_link='https://www.baidu.com',subtitle='2021年度',pos_left='20%'))
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
bar = (
Bar(init_opts=opts.InitOpts(width= "900px",height= "500px",
page_title= "abc",theme= ThemeType.CHALK,bg_color = 'black'))
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A",[5,20,36,10,75,90])
.add_yaxis('商家B',[8,34,25,14,56,100])
.set_global_opts(title_opts = opts.TitleOpts(title='销售对比',
title_link='https://www.baidu.com',subtitle='2021年度',pos_left='20%'))
)
bar.render_notebook()
- 图例配置项
class pyecharts.options.LegendOpts
.set_global_opts()
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
bar = (
Bar(init_opts=opts.InitOpts(width= "900px",height= "500px",page_title= "abc",theme= ThemeType.CHALK,bg_color = 'black'))
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A",[5,20,36,10,75,90])
.add_yaxis('商家B',[8,34,25,14,56,100])
.set_global_opts(title_opts = opts.TitleOpts(title='销售对比',title_link='https://www.baidu.com',subtitle='2021年度',pos_left='20%'),
legend_opts = opts.LegendOpts(is_show=True,pos_left="600px",orient="vertical"))
)
bar.render_notebook()
- 区域缩放配置项
class pyecharts.options.DataZoomOpts
set_global_opts()
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker # 虚假数据
bar = (
Bar(init_opts=opts.InitOpts())
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家A", Faker.days_values, color=Faker.rand_color())
.set_global_opts(
title_opts = opts.TitleOpts(title="主标题",subtitle="副标题",title_link="https://www.baidu.com/",pos_left="50%"),
legend_opts = opts.LegendOpts(is_show=True,pos_left="600px",orient="vertical"),
datazoom_opts = opts.DataZoomOpts(type_="inside")
)
)
bar.render_notebook()
- 视觉映射配置项
class pyecharts.options.VisualMapOpts
set_global_opts()
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.faker import Faker
from pyecharts.globals import ChartType
c = (
Geo()
.add_schema(maptype="广东")
.add(
"geo",
[list(z) for z in zip(Faker.guangdong_city, Faker.values())],
type_=ChartType.HEATMAP,
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(is_piecewise=False), title_opts=opts.TitleOpts(title="Geo-广东地图")
)
)
c.render_notebook()
- 工具箱配置项
class pyecharts.options.ToolboxOpts
set_global_opts()
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-显示 ToolBox"),
toolbox_opts=opts.ToolboxOpts(is_show=True),
legend_opts=opts.LegendOpts(is_show=True),
)
)
c.render_notebook()
系列配置项
.set_series_opts()
以LabelOpts:标签配置项为例
pyecharts.options.LabelOpts
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.faker import Faker
c = (
Geo()
.add_schema(maptype="china")
.add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title="Geo-基本示例")
)
)
c.render_notebook()
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker # 虚假数据
bar = (
Bar(init_opts=opts.InitOpts())
.add_xaxis(Faker.days_attrs)
.add_yaxis("商家A", Faker.days_values, color=Faker.rand_color())
# 系列配置项
.set_series_opts(label_opts=opts.LabelOpts(rotate=30))
# 全局配置项
.set_global_opts(
title_opts = opts.TitleOpts(title="主标题",subtitle="副标题",title_link="https://www.baidu.com/",pos_left="50%"),
legend_opts = opts.LegendOpts(is_show=True,pos_left="600px",orient="vertical"),
datazoom_opts = opts.DataZoomOpts(type_="inside")
)
)
bar.render_notebook()