前几天,有一个朋友对我说,pycharts做的图片好看是好看,但是没办法保存图片,感觉用起来不方便,我立马回答道,截图呗!
所以今天给大家介绍一下如何通过snapshot-selenium将pycharts做出的图导出为png格式。
首先,先从https://pypi.org/project/snapshot-selenium/#files
下载下snapshot-selenium包
然后将下载下的文件放置Anaconda下Scripts目录下
紧接着打开Anaconda Powershell Prompt
在窗口中输入snapshot-selenium文件所存放的位置,然后再利用pip命令进行安装,即可大功告成:
H:
cd H:\Users\Mloong\Anaconda3\Scripts
pip install snapshot_selenium-0.0.2-py2.py3-none-any.whl
输入以下两句代码即可调用snapshot-selenium
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
再使用make_snapshot()函数进行渲染
make_snapshot(snapshot, c.render(), "F:\\公众号\\python\\将pycharts导出为png图片\\test.png")
常见的错误!!!
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
解决方法:
这是因为输出图片时候会用到谷歌驱动器chromedriver,所以我们需要下载相应版本的chromedriver.exe,然后把它放到项目文件的目录下,具体见下
首先查看自己谷歌浏览器的版本:
打开Chrome浏览器,在地址栏中输入网址:chrome://version/
然后在http://chromedriver.storage.googleapis.com/index.html
中寻找相对应的版本
下载与自己系统相对应的chromedriver
最后,我们将解压至我们项目文件目录下即可
最后让我们来看看snapshot-selenium所渲染出的效果。
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
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-Brush示例", subtitle="我是副标题"),
brush_opts=opts.BrushOpts(),
)
)
make_snapshot(snapshot, c.render(), "F:\\公众号\\python\\将pycharts导出为png图片\\test.png")
这样我们就得到了一个透明背景的png格式了。
以下是我个人公众号,更多素材案例尽在其中,谢谢大家支持!