pyecharts可视化

pyecharts是基于Echarts图表的一个类库

Echarts是百度开源的一个可视化JavaScript库

一、pyecharts简介

pyecharts主要基于web浏览器进行显示,绘制图形(有折线图、柱状图、饼图、漏斗图、地图及极坐标图等)

安装

pip install pyecharts

二、pyecharts的使用方法

绘制图表

chart_name = Type()				# 初始化具体类型图表
chart_name.add()				# 添加数据及配置顶
cahrt_name.render()				# 生成本地文件(html,svg,jpeg,png,pdf,gif)
chart_name.render_notebook()	# 在jupyter notebook中显式

三、pyecharts常用图表

1.柱状图 Bar

常用方法

add_xaxis		# 添加x轴
add_yaxis		# 添加y轴,可设置y轴参数,也可在全局设置中设置
set_global_opts		# 全局配置设置
set_series_opts		# 系列配置设置
  • 绘制柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
%matplotlib inline

bar = (Bar()
       .add_xaxis(['衣服','裤子','鞋子','帽子'])
       .add_yaxis('价格',[70,50,60,20])
       .set_global_opts(title_opts=opts.TitleOpts(title='某服装店商品价格'))
)
bar.render_notebook()
# bar.render()# 生成html文件,render.html

链式调用

bar = Bar()
bar.add_xaxis(['衣服','裤子','鞋子','帽子'])
bar.add_yaxis('价格',[70,50,60,20])
bar.set_global_opts(title_opts=opts.TitleOpts(title='某服装店商品价格'))

pyecharts可视化_第1张图片

  • 绘制多列柱状图
bar = Bar()
bar.add_xaxis(['衣服','裤子','鞋子','帽子'])
bar.add_yaxis('A店铺',[70,50,60,20])
bar.add_yaxis('B店铺',[60,60,50,15])
# toolbox_opts=opts.ToolboxOpts(is_show=True) # pyecharts工具栏
bar.set_global_opts(title_opts=opts.TitleOpts(title='某服装店商品价格',subtitle='A和B店铺'),toolbox_opts=opts.ToolboxOpts(is_show=True))
# 设置y轴值的位置,默认中间 - - - top, right, bottom等
bar.set_series_opts(label_opts=opts.LabelOpts(position='top'))
# 绘制水平方向的直方图
# bar.reversal_axis()
bar.render()

pyecharts可视化_第2张图片

2.饼图 Pie

表现不同东西的占比情况

  • 绘制圆形饼图
from pyecharts.charts import Pie
from pyecharts import options as opts
%matplotlib inline

ls = ['apple','banana','orange','grape']
num = [15,40,20,25]
c = Pie()
# radius设置饼图半径,arg1是内圈半径,arg2是外圈半径
# center设置中心坐标
# rosetype设置南丁格尔图(玫瑰图),有radius和area两种表现形式
c.add('',[list(z) for z in zip(ls,num)],radius=[50,200])
# pos_top距离顶端的位置,pos_left距离左端的位置
c.set_global_opts(title_opts=opts.TitleOpts(title = '水果占比'),legend_opts=opts.LegendOpts(orient='vertical',pos_top='7%',pos_left='0%'))
c.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c}',font_size=20))
c.render_notebook()

pyecharts可视化_第3张图片

  • 玫瑰图
ls = ['apple','banana','orange','grape']
num = [15,40,20,25]
c = Pie()
c.add('',[list(z) for z in zip(ls,num)],radius=['30%','50%'],rosetype='radius',center=[200,200])
c.add('',[list(z) for z in zip(ls,num)],radius=['30%','50%'],rosetype='area',center=[600,200])
c.set_global_opts(title_opts=opts.TitleOpts(title = '玫瑰图'))
c.render_notebook()

pyecharts可视化_第4张图片

  • rich参数应用,可通过rich设置很多饼图属性
ls = ['apple','banana','orange','grape']
num = [15,40,20,25]
c = Pie()
c.add('',[list(z) for z in zip(ls,num)],radius=['30%','70%'],
        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=2,border_radius=4,
        rich = {'a':{'color':'red','linehight':20,'align':'center'},
                'abg':{'backgroundColor':'pink','width':'100%','align':'right','height':20,'borderRadius':[4,4,0,0]},
                'hr':{'borderColor':'blue','width':'100%','borderWidth':0.5,'height':0},
                'b':{'fontSize':16,'lineHeight':30},
                'per':{'color':'orange','backgroundColor':'balck','padding':[5,5],'borderRadius':5}
        }))
c.set_global_opts(title_opts=opts.TitleOpts(title = '水果占比'))
c.render_notebook()

pyecharts可视化_第5张图片

3.仪表盘 Gauge

from pyecharts.charts import Gauge,Page
from pyecharts import options as opts
%matplotlib inline

c = Gauge()
# c.add('速度',[('速度',44.4)],axislabel_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color=[(0.3,'#67e0e3'),(0.7,'#37a3da'),(1,'#fd666d')],width=30)))
c.add('速度',[('速度',44.4)])
c.set_global_opts(title_opts=opts.TitleOpts(title='title'),legend_opts=opts.LegendOpts(is_show=False))
c.render_notebook()

pyecharts可视化_第6张图片

4.词云 WordCloud

from pyecharts.charts import Page,WordCloud
from pyecharts.globals import SymbolType
from pyecharts import options as opts

words = [
    ('apple',243),('banana',345),('orange',425),('peach',329),
    ('white',534),('pink',784),('red',654),('blue',754)
]
c = WordCloud()
c.add('aaa',words,word_size_range=[20,70])
c.render_notebook()

pyecharts可视化_第7张图片

5.组合图表

from pyecharts.charts import Bar,Grid,Line,Scatter
from pyecharts import options as opts

a = ['小米','三星','华为','honor','魅族','vivo','oppo']
ca = [124,74,72,60,87,60,113]
b = ['草莓','苹果','橘子','柚子','西瓜','葡萄','桃子']
cb = [119,57,115,75,115,127,65]
bar = Bar()
bar.add_xaxis(a)
bar.add_yaxis('A',ca)
bar.add_yaxis('B',cb)
bar.render_notebook()
line = Line()
line.add_xaxis(b)
line.add_yaxis('',ca)
line.add_yaxis('',cb)
line.render_notebook()
grid = Grid()
# 将bar放在离底部60%远的地方,离上方40%,如果opts.GridOpts()没有传参数,两个图是重叠在一起的
grid.add(bar,grid_opts=opts.GridOpts(pos_bottom='60%'))
grid.add(line,grid_opts=opts.GridOpts(pos_top='60%'))
grid.render_notebook()

pyecharts可视化_第8张图片


散点图 Scatter
漏斗图 Funnel

K线图 Kline

你可能感兴趣的:(python,jupyter,信息可视化,pyecharts)