概况:Echarts 是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众 多开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,pyecharts 诞生了。
⭐接下来展示的是pyecharts常见绘制的图形,也是我自己对知识的一种总结。
①条形图:直观展示数据间的变化
from pyecharts.charts import Bar from pyecharts.faker import Faker from pyecharts import options as opts from pyecharts.globals import ThemeType bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK)) bar.add_xaxis(Faker.choose()) # 添加横轴数据 bar.add_yaxis('A',Faker.values()) # 添加纵轴数据 bar.add_yaxis('B',Faker.values()) # 添加纵轴数据 bar.set_global_opts(title_opts=opts.TitleOpts(title='条形图分别显示最大值')) bar.set_series_opts(markline_opts=opts.MarkLineOpts( data=[ # 线条数据 # opts.MarkLineItem(type_='min',name='最小值'), # opts.MarkLineItem(type_='max',name='最大值'), opts.MarkLineItem(type_='average',name='平均值'), ] )) bar.set_series_opts(label_opts=opts.LabelOpts(is_show=True)) bar.render_notebook()
# 堆叠条形图 from pyecharts.charts import Bar from pyecharts.faker import Faker from pyecharts import options as opts from pyecharts.globals import ThemeType bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK)) 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=True,position='inside')) bar.render_notebook()
# 横向条形图 from pyecharts.charts import Bar from pyecharts.faker import Faker from pyecharts import options as opts from pyecharts.globals import ThemeType bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK)) bar.add_xaxis(Faker.choose()) bar.reversal_axis() # 横轴反转 bar.add_yaxis('A',Faker.values()) bar.add_yaxis('B',Faker.values()) bar.set_global_opts(title_opts=opts.TitleOpts(title='横向条形图')) bar.set_series_opts(label_opts=opts.LabelOpts(position='right')) bar.render_notebook()
②折线图:查看时间维度趋势变化,可以使用折线图
from pyecharts.charts import Line from pyecharts.faker import Faker from pyecharts import options as opts from pyecharts.globals import ThemeType line = Line(init_opts=opts.InitOpts(theme=ThemeType.DARK)) line.add_xaxis(xaxis_data=Faker.choose()) # is_smooth:折线是否平滑 line.add_yaxis('A',y_axis=Faker.values(),is_smooth=True) line.add_yaxis('B',y_axis=Faker.values(),is_smooth=True) # splitline_opts:分割线设置 xaxis_opts:设置X轴 yaxis_opts:设置Y轴 line.set_global_opts(xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True))) line.set_global_opts(yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True))) line.set_global_opts(title_opts=opts.TitleOpts('平滑折线图')) line.render_notebook()
# 多数据折线图 from pyecharts.charts import Line from pyecharts.faker import Faker from pyecharts import options as opts # stack:多数据分隔开 line = Line(init_opts=opts.InitOpts(theme=ThemeType.DARK)) x_value= Faker.choose() line.add_xaxis(xaxis_data=x_value) line.add_yaxis(series_name=x_value[0],y_axis=Faker.values(),stack='总量',label_opts=opts.LabelOpts(is_show=False)) line.add_yaxis(series_name=x_value[1],y_axis=Faker.values(),stack='总量') line.add_yaxis(series_name=x_value[2],y_axis=Faker.values(),stack='总量') line.add_yaxis(series_name=x_value[3],y_axis=Faker.values(),stack='总量') line.add_yaxis(series_name=x_value[4],y_axis=Faker.values(),stack='总量') line.add_yaxis(series_name=x_value[5],y_axis=Faker.values(),stack='总量') line.set_global_opts(yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True))) line.render_notebook()
③饼图:饼图将数据表示为具有不同大小和颜色的圆的切片。并且会标记对应每个片的值也在图表中表示
from pyecharts.charts import Pie from pyecharts import options as opts from pyecharts.globals import ThemeType from pyecharts.faker import Faker pie = Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK)) # radius:设置饼图的形状,变成环形图 pie.add('',[list(z) for z in zip(Faker.choose(),Faker.values())],radius=['50%','80%']) pie.set_global_opts(title_opts=opts.TitleOpts(title='饼图的绘制')) # 设置颜色 pie.set_colors(['#FFDEAD','Honeydew','LavenderBlush','AntiqueWhite','#87CEFA','Gainsboro','FloralWhite']) # 数据的显示:{a}:系列名。 {b}:数据名。{c}:数据值。 pie.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{d}%')) pie.render_notebook()
④散点图:主要用来查看相关性
from pyecharts.charts import Scatter from pyecharts import options as opts from random import randint,random from pyecharts.globals import ThemeType sca = Scatter(init_opts=opts.InitOpts(theme=ThemeType.DARK)) x = [randint(0,100) for i in range(10)] y = [randint(0,100) for i in range(10)] sca.add_xaxis(xaxis_data=x) # symbol_size:图标大小,symbol:图标样式 sca.add_yaxis('',y_axis=y,label_opts=opts.LabelOpts(is_show=True),symbol_size=10,symbol='circle') sca.set_global_opts( title_opts=opts.TitleOpts(title='散点图'), xaxis_opts=opts.AxisOpts(type_='value'), # 只显示值 visualmap_opts=opts.VisualMapOpts(type_='size')) # 改变颜色 sca.set_colors('AntiqueWhite') sca.render_notebook()
⑤热力图:热图用颜色比较分类数据,可通过设置表单元格的大小和形状来增强这种基本热图,(比如大小越大颜色越深代表销售额越高)颜色图例反映出连续数据范围
from pyecharts.charts import HeatMap from random import randint,random from pyecharts.globals import ThemeType from pyecharts.faker import Faker from pyecharts import options as opts hm = HeatMap(init_opts=opts.InitOpts(theme=ThemeType.INFOGRAPHIC)) # 构造x轴数据 横向24小时,纵向7天 value = [[i,j, randint(0,40)] for i in range(24) for j in range(7)] hm.add_xaxis(Faker.choose()) 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(type_='color',range_color=['lightblue','orange'])) hm.render_notebook()
⑥漏洞图:漏斗图适用于业务流程比较规范、周期长、环节多的流程分析,通过漏斗各环节业务数据的比较,能够直观地发现和说明问题所在
from pyecharts.charts import Funnel from pyecharts.globals import ThemeType from pyecharts.faker import Faker from pyecharts import options as opts funnel = Funnel(init_opts=opts.InitOpts(theme=ThemeType.DARK)) funnel.add('',[list(z) for z in zip(Faker.choose(),Faker.values())]) funnel.set_global_opts(title_opts=opts.TitleOpts('漏斗图')) # sort不设置:默认倒漏斗,sort='ascending':正漏斗 funnel.set_series_opts(label_opts=opts.LabelOpts(is_show=True,position='outside',formatter='{b}: {d}%'),sort='ascending') funnel.render_notebook()
⑦雷达图(Radar Chart):又称为蜘蛛网图(Spider Chart),雷达图的优势是能够表达多个指标的对比情况。当我们需要做多维度对比分析时,雷达图是个不错的选择
from pyecharts.charts import Radar from pyecharts import options as opts from pyecharts.faker import Faker radar = Radar(init_opts=opts.InitOpts(theme=ThemeType.HALLOWEEN)) # 添加数据 score1 = [[83,73,83,81,98,70,57]] score2 = [[93,55,76,87,63,72,100]] radar.add_schema( schema=[ opts.RadarIndicatorItem(name='语文',max_=100), opts.RadarIndicatorItem(name='数学',max_=100), opts.RadarIndicatorItem(name='英语',max_=100), opts.RadarIndicatorItem(name='物理',max_=100), opts.RadarIndicatorItem(name='化学',max_=100), opts.RadarIndicatorItem(name='地理',max_=100), opts.RadarIndicatorItem(name='政治',max_=100) ]) # linestyle_opts:线条颜色配置 radar.add('张三',score1,linestyle_opts=opts.LineStyleOpts(color="#5CACEE")) radar.add('李四',score2,linestyle_opts=opts.LineStyleOpts(color="#CD0000"),) radar.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) radar.set_global_opts(title_opts=opts.TitleOpts('雷达图')) radar.set_colors(['green','white']) # 设置图例的颜色 radar.render_notebook()
⑧坐标地图:地图与路线的结合
from pyecharts.charts import Geo from pyecharts.globals import ThemeType,GeoType,SymbolType import pyecharts.options as opts geo = Geo(init_opts=opts.InitOpts(theme=ThemeType.DARK)) geo.add_schema() geo.add('', [['北京','新疆'],['北京','深圳'],['西藏','贵州'],['上海','深圳']], label_opts=opts.LabelOpts(is_show=False), # 类型设置:显示路线 type_=GeoType.LINES, # 外部的线颜色 color='blue', # 增加弧度 linestyle_opts=opts.LineStyleOpts(curve=0.3), # 箭头的颜色 effect_opts=opts.EffectOpts(symbol=SymbolType.ARROW,symbol_size=18,color='lightblue')) geo.set_global_opts(title_opts=opts.TitleOpts(title='坐标地图'),visualmap_opts=opts.VisualMapOpts()) geo.render_notebook()
⑨时间线轮播多图:每个时间段数据的变化
from pyecharts.charts import Bar,Timeline from pyecharts.faker import Faker from pyecharts import options as opt from matplotlib.pyplot import ylabel x = Faker.choose() tl = Timeline(init_opts=opts.InitOpts(theme=ThemeType.DARK)) for i in range(2007,2022): bar = Bar() bar.add_xaxis(x) bar.add_yaxis('A',Faker.values(),color=Faker.rand_color()) bar.add_yaxis('B',Faker.values(),color=Faker.rand_color()) bar.set_global_opts(title_opts=opt.TitleOpts(title=f'{i}年销售量')) tl.add(bar,f'{i}年') tl.render_notebook()
⑩仪表盘图:查看指标完成的程度
from pyecharts.charts import Gauge from pyecharts import options as opt c = Gauge(init_opts=opts.InitOpts(theme=ThemeType.DARK)) c.add("", [("完成率", 70)],split_number=10, # 分割的段数 axisline_opts=opt.AxisLineOpts(linestyle_opts=opt.LineStyleOpts( color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30))) c.set_global_opts(title_opts=opts.TitleOpts(title="仪表盘图")) c.render_notebook()
标签:基本,Pyecharts,pyecharts,bar,Faker,add,使用,import,opts From: https://www.cnblogs.com/lxxduang/p/16757716.html