pyecharts官方示例如下:
https://gallery.pyecharts.org/#/Bar/bar_base
代码:
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.options.global_options import AxisOpts
# 导入输出图片工具
from pyecharts.render import make_snapshot
# 使用snapshot-selenium 渲染图片
from snapshot_selenium import snapshot
import pandas as pd
import numpy as np
import xlsxwriter as xlwt
import openpyxl
def make_bar(x,y):
#x = ['餐饮', '娱乐', '交通', '保养', '衣服']
#y1 = [1000, 500, 100, 5000, 5000]
#y2 = [2000, 1000, 100, 20, 30]
bar = Bar()
bar.add_xaxis(xaxis_data=x)
# 第一个参数是图例的名称
bar.add_yaxis(series_name='1', y_axis=y)
#bar.add_yaxis(series_name='2', y_axis=y2)
# 添加options
bar.set_global_opts(title_opts=opts.TitleOpts(title='柱状图示例'))
# 生成HTML文件保存在当前目录
# 保存图片
make_snapshot(snapshot, bar.render('datatest.html'), "datatest.png")
def main():
# 读取excel数据
path = r'C:\Users\hp\Desktop\dataset.xlsx'
temp_data = pd.read_excel(path, sheet_name='六、主观自我评价', engine='openpyxl')
y_list=[]
Closewindows= temp_data['第2组测评分']
len_data=10
# 遍历提取出来的数据,插入到list中。取10个数据
for i, data in enumerate(Closewindows):
# print(i) 序号是0-936一共937个数据。
if i<10:
y_list.append(data)
#调用绘制柱状图的函数
make_bar(len_data,y_list)
if __name__ == '__main__':
main()
中途保存图片的时候报错了:
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH.
原因是用了谷歌浏览器去渲染图片,但是谷歌浏览器没有驱动。
解决参考:
https://blog.csdn.net/u011318077/article/details/90113206