本文主要介绍如何利用Pyecharts来绘制一些常用的可视化图形,比如散点图、饼图、漏斗图等等,具体的绘制方法请见下文。
1.绘制散点图
pyecharts使用Scatter绘制散点图。
from pyecharts import options as opts from pyecharts.charts import Scatter week = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"] c = Scatter() c.add_xaxis(week) c.add_yaxis("商家A", [81,65,48,32,68,92,87]) c.set_global_opts(title_opts=opts.TitleOpts(title="Scatter-一周的销售额(万元)")) c.render_notebook()
结果图:
2.绘制饼图
饼图常用于表示不同类别的占比情况。使用Pie()方法可以绘制饼图。
2.1绘制实心饼图
from pyecharts import options as opts from pyecharts.charts import Page, Pie L1=['教授','副教授','讲师','助教','其他'] num = [20,30,10,12,8] c = Pie() c.add("", [list(z) for z in zip(L1,num)]) c.set_global_opts(title_opts=opts.TitleOpts(title="Pie-职称类别比例")) c.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) c.render_notebook()
结果图:
2.2 绘制圆形饼图
from pyecharts import options as opts from pyecharts.charts import Page, Pie wd = ['教授','副教授','讲师','助教','其他'] num = [20,30,10,12,8] c = Pie() c.add("",[list(z) for z in zip(wd, num)],radius = ["40%", "75%"]) # 圆环的粗细和大小 c.set_global_opts( title_opts=opts.TitleOpts(title="Pie-Radius"),legend_opts=opts.LegendOpts( orient="vertical", pos_top="5%", pos_left="2%" )) c .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) c.render_notebook()
结果图:
2.3 绘制玫瑰图
from pyecharts import options as opts from pyecharts.charts import Page, Pie data1 = [45,86,39,52,68] data2 = [67,36,64,89,123] labels = ['电脑','手机','彩电','冰箱','洗衣机'] c = Pie() c.add("",[list(z) for z in zip(labels, data1)],radius=["35%", "70%"],center=[250,220],rosetype='radius') c.add("",[list(z) for z in zip(labels, data2)],radius=["35%", "70%"],center=[650,240],rosetype='area') c.set_global_opts(title_opts=opts.TitleOpts(title="玫瑰图")) c.render_notebook()
结果图:
3.绘制漏斗图
from pyecharts.charts import Funnel from pyecharts import options as opts %matplotlib inline data = [45,86,39,52,68] labels = ['电脑','手机','彩电','冰箱','洗衣机'] wf = Funnel() wf.add('电器销量图',[list(z) for z in zip(labels, data)], is_selected= True) wf.render_notebook()
结果图:
4.绘制仪表盘
from pyecharts import options as opts from pyecharts.charts import Gauge data = [("完成率", 60)] gauge = ( Gauge() .add( "仪表盘名称", data, title_label_opts=opts.LabelOpts( position="inside" # 将指标名称放在仪表盘内部 ), detail_label_opts=opts.GaugeDetailOpts( offset_center=[0, "40%"] # 将数据值放在仪表盘上方 ), ) .set_global_opts( title_opts=opts.TitleOpts(title="仪表盘标题"), legend_opts=opts.LegendOpts(is_show=False), ) ) gauge.render_notebook()
结果图:
5.绘制组合图表
from pyecharts import options as opts from pyecharts.charts import Bar, Grid, Line,Scatter A = ["小米", "三星", "华为", "苹果", "魅族", "VIVO", "OPPO"] CA = [100,125,87,90,78,98,118] B = ["草莓", "芒果", "葡萄", "雪梨", "西瓜", "柠檬", "车厘子"] CB = [78,95,120,102,88,108,98] bar = Bar() bar.add_xaxis(A) bar.add_yaxis("商家A",CA) bar.add_yaxis("商家B", CB) bar.set_global_opts(title_opts=opts.TitleOpts(title="Grid-Bar")) bar.render_notebook() line=Line() line.add_xaxis(B) line.add_yaxis("商家A", CA) line.add_yaxis("商家B", CB) line.set_global_opts(title_opts=opts.TitleOpts(title="Grid-Line", pos_top="48%"), legend_opts=opts.LegendOpts(pos_top="48%")) line.render_notebook() grid = Grid() grid.add(bar, grid_opts=opts.GridOpts(pos_bottom="60%")) grid.add(line, grid_opts=opts.GridOpts(pos_top="60%")) grid.render_notebook()
结果图: