需要源码请点赞关注收藏后评论区留言私信~~~
一、pyecharts简介
pyecharts是基于Echart图表的一个类库,而Echart是百度开源的一个可视化JavaScript库
pyecharts主要基于web浏览器进行显示,绘制的图形比较多,包括折线图、柱状图、饼图、漏斗图、地图、极坐标图等,代码量很少,而且很灵活,绘制出来的图形很美观
使用pyecharts时,需要安装相应的库,安装命令为: pip install pyecharts
图形绘制过程,基本上所有的图表类型都是这样绘制的
chart_name = Type() 初始化具体类型图表
chart_name .add() 添加数据及配置项
chart_name .render() 生成本地文件(
html/svg/jpeg/png/pdf/gif) chart_name .render_notebook 在jupyter notebook中显示
二、pyechart常用图表
1 柱状图
利用Bar方法可以绘制柱状图。方法及说明见表8-1。
表8-1 Bar对象的主要方法及其说明
from pyecharts.charts import Bar
from pyecharts import options as opts
%matplotlib inline
# V1 版本开始支持链式调用
bar = ( Bar()
.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
.set_global_opts(title_opts = opts.TitleOpts(title = "某商场销售情况")) )
bar.render_notebook()
#bar.render() 生成html
V1版本开始支持链式调用,如果不习惯链式调用的开发者依旧可以单独调用方法
使用多个add_yaxis可以绘制并列柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
%matplotlib inline
bar = Bar()
bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
bar.set_global_opts(title_opts = opts.TitleOpts(title = "货品销售情况",subtitle = "A和B公司"))
bar.render_notebook()
利用bar.reversal_axis()可以绘制水平的直方图
%matplotlib inline
bar = Bar()
bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
bar.set_global_opts(title_opts=opts.TitleOpts(title="货品销售情况",subtitle = "A和B公司"),toolbox_opts = opts.ToolboxOpts(is_show = True))
bar.set_series_opts(label_opts=opts.LabelOpts(position = "right"))
bar.reversal_axis()
bar.render_notebook()
2 饼图
饼图常用于表现不同类别的占比情况。利用Pie方法可以绘制饼图
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()
通过参数圆形饼图radius可以绘制圆形饼图
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()
可以通过rich参数设置更多属性
c = Pie()
c.add("",[list(z) for z in zip(wd, num)],radius=["40%", "55%"],
label_opts=opts.LabelOpts(position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n{b|{b}:}{c} {per|{d}%} ",
background_color="#eee",border_color="#aaa",
border_width=1,border_radius=4,
rich={"a": {"color": "#999", "lineHeight": 22, "align": "center"},"abg": {"backgroundColor": "#e3e3e3","width": "100%", "align": "right", "height": 22,"borderRadius": [4, 4, 0, 0],}, "hr": {"borderColor": "#aaa", "width": "100%","borderWidth": 0.5, "height": 0,}, "b": {"fontSize": 16, "lineHeight": 33},"per": {"color": "#eee","backgroundColor": "#334455", "padding": [2, 4],"borderRadius": 2,} } ))
c.set_global_opts(title_opts=opts.TitleOpts(title="Pie-富文本示例"))
c.render_notebook()
玫瑰图绘制
from pyecharts import options as opts
from pyecharts.charts import Page, Pie
data = [45,86,39,52,68]
labels = ['电脑','手机','彩电','冰箱','洗衣机']
c = Pie()
c.add("",[list(z) for z in zip(labels, data)],radius=["40%", "55%"],center=[240,220],rosetype='radius')
c.add("",[list(z) for z in zip(wd, num)],radius=["40%", "55%"],center=[620,220],rosetype='area')
c.set_global_opts(title_opts=opts.TitleOpts(title="玫瑰图"))
c.render_notebook()
3 漏斗图
pyecharts中通过Funnel绘制漏斗图
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 散点图
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()
5 K线图
pyecharts利用Kline绘制K线图
from pyecharts import options as opts
from pyecharts.charts import Kline
data = [[2320.26, 2320.26, 2287.3, 2362.94],[2300, 2291.3, 2288.26, 2308.38],[2295.35, 2346.5, 2295.35, 2345.92],--------]
c = Kline()
c.add_xaxis(["2019/7/{}".format(i + 1) for i in range(31)])
c.add_yaxis("2019年7月份K线图", data)
c.set_global_opts(yaxis_opts=opts.AxisOpts(is_scale=True),
xaxis_opts=opts.AxisOpts(is_scale=True),
title_opts=opts.TitleOpts(title="Kline-基本示例"),)
c.render_notebook()
6 仪表盘
pyecharts利用Gauge绘制仪表盘
from pyecharts import options as opts
from pyecharts.charts import Gauge, Page
c = Gauge()
c.add("业务指标",[("完成率", 55.5)],axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color=[(0.3, "#67e0e3"),
(0.7, "#37a2da"), (1, "#fd666d")], width=30)))
c.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-不同颜色"),
legend_opts=opts.LegendOpts(is_show=False))
c.render_notebook()
7 词云
Pyecharts利用WordCloud绘制词云
文章来源:https://www.toymoban.com/news/detail-430120.html
from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType
words = [
("牛肉面", 7800),("黄河", 6181),
("《读者》杂志", 4386), ("甜胚子", 3055),
---------,
("五泉山", 2550)]
c = WordCloud()
c.add("", words, word_size_range=[20, 80])
c.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
c.render_notebook()
8 组合图表
组合图表上下布局
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()
9 桑基图
桑基图(Sankey diagram)即桑基能量分流图,也叫桑基能量平衡图。它是一种特定类型的流程图,图中延伸的分支的宽度对应数据流量的大小,通常应用于能源、材料成分、金融等数据的可视化分析。Pyecharts中利用Sankey绘制桑基图
下面对熬夜原因进行桑基图可视化
10 平行坐标图
平行坐标图(Parallel Coordinates Plot)是对于具有多个属性问题的一种可视化方法。在平行坐标图中,数据集的一行数据在平行坐标图中用一条折线表示,纵向是属性值,横向是属性类别(用索引表示)
图是一种抽象的数据结构,可以描述最广泛的数据之间的关系,例如交通网络、微信中的朋友关系网,在图中,最基本的单元是顶点,顶点之间的关联关系称为边,pyecharts可以利用Graph方法绘制图
11 地图
自从 0.3.2 开始,为了缩减项目本身的体积以及维持 pyecharts 项目的轻量化运行,pyecharts 将不再自带地图 js 文件。如用户需要用到地图图表(Geo、Map),可自行安装对应的地图文件包。 Windows下通过以下的pip命令进行安装: #安装全球国家地图,包括世界地图和 213 个国家 pip install echarts-countries-pypkg #安装中国省级地图,包括23 个省,5 个自治区 pip install echarts-china-provinces-pypkg #中国市级地图,包括370 个中国城市 pip install echarts-china-cities-pypkg
下面在指定地图上的城市标识某天最高温度 效果如下 点击对应城市即可显示温度
from pyecharts import options as opts
from pyecharts.charts import Map
temperature=[30,31,27,29,18]
loc = ['兰州市','天水市','白银市','武威市','甘南藏族自治州']
c = Map()
c.add("甘肃省", [list(z) for z in zip(loc, temperature)], "甘肃",is_roam=True)
# is_roam是否开启鼠标缩放和平移漫游
c .set_global_opts(title_opts=opts.TitleOpts(title="甘肃省部分城市最高气温"))
c.render("APP类型.html")
c.render_notebook()
创作不易 觉得有帮助请点赞关注收藏~~~文章来源地址https://www.toymoban.com/news/detail-430120.html
到了这里,关于【数据分析与可视化】pyecharts可视化图表讲解及实战(超详细 附源码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!