pyecharts安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts
问题: pyecharts v1.9.0 版本功能预警
#PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 😃
#super().init(init_opts=init_opts)
原因:
所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置!
原因:我们安装的pyecharts版本是最新版本1.8.1,而很多官网实例是基于1.0.0版本而设计的,故而我们需要更换pyecharts版本
解决方法
步骤1:先卸载安装过的最新版的pyecharts:
pip uninstall pyecharts
卸载成功提示: Successfully uninstalled pyecharts-1.8.1
步骤2:
#输入指定版本1.7.1:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts==1.0.0
pyecharts学习官网
https://pyecharts.org/#/官网
官网实例
#!/user/bin/env python
#-*-coding: utf-8-*-
#@Time : 2020/9/19 00196:34
#@Author : GodSpeed
#@File : pyecharts_lxy.py
#@Software : PyCharm
import pyecharts.options as opts # 导入配置项
from pyecharts.charts import Line # 导入图表类
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://www.echartsjs.com/examples/editor.html?c=line-log
目前无法实现的功能:
1、暂无
"""
x_data = ["一", "二", "三", "四", "五", "六", "七", "八", "九"]
y_data_3 = [1, 3, 9, 27, 81, 247, 741, 2223, 6669]
y_data_2 = [1, 2, 4, 8, 16, 32, 64, 128, 256]
y_data_05 = [1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 32, 1 / 64, 1 / 128, 1 / 256, 1 / 512]
(
Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="1/2的指数",
y_axis=y_data_05,
linestyle_opts=opts.LineStyleOpts(width=2),
)
.add_yaxis(
series_name="2的指数", y_axis=y_data_2, linestyle_opts=opts.LineStyleOpts(width=2)
)
.add_yaxis(
series_name="3的指数", y_axis=y_data_3, linestyle_opts=opts.LineStyleOpts(width=2)
)
.set_global_opts(
title_opts=opts.TitleOpts(title="对数轴示例", pos_left="center"),
tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b} : {c}"),
legend_opts=opts.LegendOpts(pos_left="left"),
xaxis_opts=opts.AxisOpts(type_="category", name="x"),
yaxis_opts=opts.AxisOpts(
type_="log",
name="y",
splitline_opts=opts.SplitLineOpts(is_show=True),
is_scale=True,
),
)
.render("log_axis.html")
)
# 运行会生成log_axis.html文件
# 运行后不会直接显示图,会渲染到本地的网页呈现
# 直接渲染到pycharm
c = (
Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="1/2的指数",
y_axis=y_data_05,
linestyle_opts=opts.LineStyleOpts(width=2),
)
.add_yaxis(
series_name="2的指数", y_axis=y_data_2, linestyle_opts=opts.LineStyleOpts(width=2)
)
.add_yaxis(
series_name="3的指数", y_axis=y_data_3, linestyle_opts=opts.LineStyleOpts(width=2)
)
.set_global_opts(
title_opts=opts.TitleOpts(title="对数轴示例", pos_left="center"),
tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b} : {c}"),
legend_opts=opts.LegendOpts(pos_left="left"),
xaxis_opts=opts.AxisOpts(type_="category", name="x"),
yaxis_opts=opts.AxisOpts(
type_="log",
name="y",
splitline_opts=opts.SplitLineOpts(is_show=True),
is_scale=True,
),
)
#.render("log_axis.html")
)
c.render_notebook()
import pyecharts.options as opts # 导入配置项
from pyecharts.charts import Line # 导入折线图类
# from pyecharts.faker import Faker
c = (
Line() # 创建折线图对象
.add_xaxis([1,2,3,4]) # 添加x轴,数据
.add_yaxis("商家A", [1,2,3,4], is_smooth=True) # 添加y轴:图例,y数据
.add_yaxis("商家B", [5,6,7,8], is_smooth=True) # 添加y轴:图例,数据
.set_global_opts(title_opts=opts.TitleOpts(title="Line-smooth")) # 全局配置项,指定标题
# .render("line_smooth.html") # 渲染到本地的网页呈现
)
c.render_notebook() # 渲染到编辑器中
注意:
在使用pyecharts过程中,它的数据一定是python原生的list