安装pyecharts
pip install pyecharts
安装snapshot_selenium
pip install snapshot_selenium
举例
from pyecharts import options as opts
from pyecharts.globals import ThemeType
# 导入主题:https://pyecharts.org/#/zh-cn/themes
# .LIGHT、.DARK、.CHALK、.ESSOS、.INFOGRAPHIC、.PURPLE_PASSION ...
from pyecharts.charts import Bar
# 导入柱状图
from pyecharts.render import make_snapshot
# 使用 snapshot-selenium 渲染图片
from snapshot_selenium import snapshot
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
x = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
# 创建变量数据
bar = (
Bar(opts.InitOpts(width = '1000px',height = '500px',theme=ThemeType.INFOGRAPHIC))
.add_xaxis(x)
.add_yaxis("商家A", v1,is_selected = True)
.add_yaxis("商家B", v2,is_selected = True)
.set_global_opts(
# 设置全局参数
title_opts = opts.TitleOpts(title="商场数据显示"), # 设置title
xaxis_opts= opts.AxisOpts(
splitline_opts=opts.SplitLineOpts(is_show=True)
), # 设置x轴
yaxis_opts= opts.AxisOpts(
splitarea_opts=opts.SplitAreaOpts(is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1))
), # 设置y轴
toolbox_opts = opts.ToolboxOpts(is_show = True), # 设置工具箱
datazoom_opts= [opts.DataZoomOpts(range_start=10, range_end=80,is_zoom_lock=False)], # 设置slider
)
.set_series_opts(
# 设置系列配置
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
]
),
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_="average", name="平均值"),
]
),
)
)
make_snapshot(snapshot, bar.render(), "bar.gif")
运行时报错
WebDriverException: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
解决方法
查看电脑上chrome版本
在Chrome浏览器地址栏输入chrome://version/回车后页面会显示浏览器版本和安装目录。
chromedriver的下载地址:http://chromedriver.storage.googleapis.com/index.html
下载一个匹配版本的zip文件, 解压后是chromedriver.exe文件。
将chromedriver.exe 复制到chrome和python安装目录下
先将hromedriver.exe 复制到chrome安装的Application文件下,chrome的路径为C:\Program Files (x86)\Google\Chrome\Application
然后将hromedriver.exe 复制到python安装文件下,我用的是Aanaconda, 所以放在Aanaconda根目录下,
添加环境变量
打开开始菜单->我的电脑(或计算机)->系统属性->高级系统设置->环境变量,(win10 可以直接在左下搜索框输入 “环境变量”, 编辑用户变量里的Path,新建C:\Program Files (x86)\Google\Chrome\Application; 修改完之后点击确定按钮保存配置。
重新运行代码即可。
要保存为其他格式的图片,只需修改保存图片的格式:
make_snapshot(snapshot, bar.render(), "bar.gif")
"bar.gif" "bar.png"