第一种:生成HTML文件
以柱形图为例
# 导入柱状图-Bar from pyecharts.charts import Bar from pyecharts import options as opts # 设置行名 columns = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] # 设置列数据 data1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3] data2 = [1.2,3.4,5.4,3.3,4.3,5.6,76.8,54.5,5,4,7.6,4.7] # bar是实例对象 bar = Bar() # x轴数据 bar.add_xaxis(xaxis_data=columns) # 第一个参数是图例名称,第二个参数是y轴数据 bar.add_yaxis(series_name= "平台A",yaxis_data=data1) bar.add_yaxis(series_name="平台B",yaxis_data=data2) # 设置表的名称 bar.set_global_opts(title_opts=opts.TitleOpts(title='不同平台分析表')) # x轴和y轴转换 # bar.reversal_axis() # 生成html文件 bar.render('first_bar.html')
运行效果
第二种:内嵌展示
在jupyter notebook中,通过内嵌展示可以直接显示出来
# 导入柱状图-Bar from pyecharts.charts import Bar from pyecharts import options as opts # 设置行名 columns = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] # 设置列数据 data1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3] data2 = [1.2,3.4,5.4,3.3,4.3,5.6,76.8,54.5,5,4,7.6,4.7] # bar是实例对象 bar = Bar() # x轴数据 bar.add_xaxis(xaxis_data=columns) # 第一个参数是图例名称,第二个参数是y轴数据 bar.add_yaxis(series_name= "平台A",yaxis_data=data1) bar.add_yaxis(series_name="平台B",yaxis_data=data2) # 设置表的名称 bar.set_global_opts(title_opts=opts.TitleOpts(title='不同平台分析表')) # jupyter内嵌展示 bar.render_notebook()
运行结果一样
第三种:生成图片文件
在生成图片之前需要两件事情
1.安装软件
pip install snapshot_selenium
采用chromedriver进行图片操作
2.配置环境变量
将chromedriver驱动放到PATHua环境变量下
见链接:
https://blog.csdn.net/shenfuli/article/details/94401967
from snapshot_selenium import snapshot as driver from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.render import make_snapshot def bar_chart() ->Bar: # 设置行名 columns = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] # 设置列数据 data1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3] data2 = [1.2, 3.4, 5.4, 3.3, 4.3, 5.6, 76.8, 54.5, 5, 4, 7.6, 4.7] # bar是实例对象 c = Bar() # x轴数据 c.add_xaxis(xaxis_data=columns) # 第一个参数是图例名称,第二个参数是y轴数据 c.add_yaxis(series_name="平台A", yaxis_data=data1) c.add_yaxis(series_name="平台B", yaxis_data=data2) # 设置表的名称 c.set_global_opts(title_opts=opts.TitleOpts(title='不同平台分析表')) return c c = bar_chart() make_snapshot(driver,c.render(),"first.png")
运行结果