PyEcharts 简介
Echarts 是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众 多开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇 上数据可视化时,pyecharts 诞生了。
特性
• 简洁的 API 设计,使用如丝滑般流畅,支持链式调用
• 囊括了 30+ 种常见图表,应有尽有
• 支持主流 Notebook 环境,Jupyter Notebook 和 JupyterLab
• 可轻松集成至 Flask,Django 等主流 Web 框架
• 高度灵活的配置项,可轻松搭配出精美的图表
• 详细的文档和示例,帮助开发者更快的上手项目
• 多达 400+ 地图文件以及原生的百度地图,为地理数据可视化提供强有力的支持
Bar图
from pyecharts.charts import Bar from pyecharts.faker import Faker from pyecharts import options as opts bar = Bar() bar.add_xaxis(Faker.choose()) bar.add_yaxis('销售团队A',Faker.values()) bar.add_yaxis('销售团队B',Faker.values()) bar.set_series_opts(markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_='max',name='最大值')] )) bar.render_notebook()
from pyecharts.charts import Bar from pyecharts.faker import Faker from pyecharts import options as opts bar = Bar() bar.add_xaxis(Faker.choose()) bar.add_yaxis('销售团队A',Faker.values()) bar.add_yaxis('销售团队B',Faker.values()) bar.reversal_axis() bar.set_series_opts(label_opts=opts.LabelOpts(position="right")) bar.set_global_opts(title_opts=opts.TitleOpts(title="XY翻转")) bar.render_notebook()
from pyecharts.charts import Bar from pyecharts.faker import Faker from pyecharts import options as opts bar = Bar() bar.add_xaxis(Faker.choose()) bar.add_yaxis('销售团队A',Faker.values(),stack='stack') bar.add_yaxis('销售团队B',Faker.values(),stack='stack') bar.set_global_opts(title_opts=opts.TitleOpts(title="堆叠图")) bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) bar.render_notebook()
Line图
from pyecharts.charts import Line from pyecharts import options as opts from pyecharts.faker import Faker line = Line() line.add_xaxis(xaxis_data=Faker.choose()) line.add_yaxis('商家A',y_axis=Faker.values()) line.add_yaxis('商家B',y_axis=Faker.values()) line.set_global_opts(xaxis_opts=opts.AxisOpts(splitline_opts = opts.SplitLineOpts(is_show=True))) line.render_notebook()
from pyecharts.charts import Line from pyecharts import options as opts from pyecharts.faker import Faker line = Line() line.add_xaxis(xaxis_data=Faker.choose()) line.add_yaxis('商家A',y_axis=Faker.values(),is_smooth = True) line.add_yaxis('商家B',y_axis=Faker.values(),is_smooth = True) line.set_global_opts(xaxis_opts=opts.AxisOpts(splitline_opts = opts.SplitLineOpts(is_show=True))) line.render_notebook()
from pyecharts.charts import Line from pyecharts import options as opts from pyecharts.faker import Faker line = Line() x_values = Faker.choose() line.add_xaxis(x_values) line.add_yaxis( series_name=x_values[0], y_axis=Faker.values(), stack="总量", label_opts= opts.LabelOpts(is_show=False), areastyle_opts = opts.AreaStyleOpts(opacity=0.5) ) line.add_yaxis( series_name=x_values[1], y_axis=Faker.values(), stack="总量", label_opts= opts.LabelOpts(is_show=False), areastyle_opts = opts.AreaStyleOpts(opacity=0.5)) line.add_yaxis( series_name=x_values[2], y_axis=Faker.values(), stack="总量", label_opts= opts.LabelOpts(is_show=False), areastyle_opts = opts.AreaStyleOpts(opacity=0.5)) line.add_yaxis( series_name=x_values[3], y_axis=Faker.values(), stack="总量", label_opts= opts.LabelOpts(is_show=False), areastyle_opts = opts.AreaStyleOpts(opacity=0.5)) line.add_yaxis( series_name=x_values[4], y_axis=Faker.values(), stack="总量", label_opts= opts.LabelOpts(is_show=False), areastyle_opts = opts.AreaStyleOpts(opacity=0.5)) line.set_global_opts(xaxis_opts=opts.AxisOpts(boundary_gap=False)) line.render_notebook()
Pie图
from pyecharts.charts import Pie from pyecharts import options as opts from pyecharts.faker import Faker pie = Pie() pie.add('',[list(z) for z in zip(Faker.choose(),Faker.values())]) pie.set_global_opts(title_opts=opts.TitleOpts(title="Pie的基本图表")) pie.render_notebook()
from pyecharts.charts import Pie from pyecharts import options as opts from pyecharts.faker import Faker pie = Pie() pie.add('',[list(z) for z in zip(Faker.choose(),Faker.values())],radius=['50%','70%']) pie.set_global_opts(title_opts=opts.TitleOpts(title="Pie的基本图表")) pie.render_notebook()
pie = Pie() pie.add('',[list(z) for z in zip(Faker.choose(),Faker.values())],rosetype='radius') pie.set_global_opts(title_opts=opts.TitleOpts(title="Pie的基本图表")) pie.render_notebook()
Scatter图
from pyecharts.charts import Scatter from pyecharts import options as opts from random import random,randint x = [randint(0,100) for i in range(100)] y = [randint(0,100) for i in range(100)] sca = Scatter() sca.add_xaxis(xaxis_data=x) sca.add_yaxis('',y_axis=y,label_opts=opts.LabelOpts(is_show=False),symbol_size=10,symbol='rect') sca.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value')) sca.render_notebook()
from pyecharts.charts import Scatter from pyecharts import options as opts from random import random,randint x = [randint(0,100) for i in range(10)] y = [randint(0,100) for i in range(10)] sca = Scatter() sca.add_xaxis(xaxis_data=x) sca.add_yaxis('',y_axis=y,label_opts=opts.LabelOpts(is_show=True)) sca.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value') ,visualmap_opts=opts.VisualMapOpts(type_='size')) sca.render_notebook()
Boxplot图
from pyecharts.charts import Boxplot from pyecharts import options as opts from random import randint box = Boxplot() box.add_xaxis([f'{i}月' for i in range(1,5)]) box.add_yaxis('A',box.prepare_data([ [randint(50,80) for i in range(50)], [randint(70,100) for i in range(50)], [randint(50,90) for i in range(50)], [randint(50,120) for i in range(50)], ])) box.set_global_opts(title_opts=opts.TitleOpts(title='箱图的基本案例')) box.render_notebook()
heatmap图
from pyecharts.charts import HeatMap from pyecharts import options as opts from pyecharts.faker import Faker from random import randint from pyecharts.types import VisualMap value = [[i,j, randint(0,40)] for i in range(24) for j in range(7)] hm = HeatMap() hm.add_xaxis(Faker.clock) hm.add_yaxis('',Faker.week,value,label_opts=opts.LabelOpts(is_show=True,position='inside')) hm.set_global_opts(title_opts=opts.TitleOpts(title='热力图'), visualmap_opts= opts.VisualMapOpts() ) hm.render_notebook()