亲宝软件园·资讯

展开

使用pyecharts1.7进行简单的可视化大全

theskylife 人气:3

近期,又有接触到pyecharts这个包的使用,后面发现这个曾经好用的包发生了一些变化,为了方便大家的使用,这里整理如下:
绘图风格theme:默认WHITE
LIGHT, DARK, WHITE, CHALK, ESSOS, INFOGRAPHIC, MACARONS, PURPLE_PASSION, ROMA, ROMANTIC, SHINE, VINTAGE, WALDEN, WESTEROS, WONDERLAND

1.柱状图绘制

1.1 最基础的柱状图

from pyecharts.charts import Bar,Grid
from pyecharts import options as opts
from pyecharts.globals import ThemeType
import random
import numpy as np
# 准备数据
name=["A","B","C","D"]
salery=[random.randint(3000,5000) for i in range(4)]
#绘图
bar=Bar(init_opts = opts.InitOpts(width='600px',height='400px')) 
bar.add_xaxis(name)
bar.add_yaxis("salery",salery)
bar.set_global_opts(title_opts=opts.TitleOpts(title="收入情况"))
#仅在notebook中显示
bar.render_notebook()
#在HTML中显示
bar.render("收入情况")

效果图:

1.2 稍微复杂的柱状图

为了减少代码量,此处不再导入包。绘制收入和消费情况,并使用新风格,并添加副标题,使用新版本的链式写法。

#准备数据
name=["A","B","C","D"]
salery=[random.randint(3000,5000) for i in range(4)]
cost=[random.randint(1000,2000) for i in range(4)]
#绘图
bar=(
  Bar(init_opts = opts.InitOpts(width='600px',height='400px',theme=ThemeType.LIGHT))
  .add_xaxis(name)
  .add_yaxis("salery",salery)
  .add_yaxis("cost",cost)
  .set_global_opts(title_opts=opts.TitleOpts(title="收入及消费情况",subtitle="随机样本"))
)
bar.render_notebook()

#效果图:

1.3 堆叠式柱状图

使用堆叠式柱状图(部分堆叠),并自定义颜色,修改图例的显示位置,不显示数字,改变背景颜色

#准备数据
name=["A","B","C","D"]
salery=[random.randint(3000,5000) for i in range(4)]
cost=[random.randint(1000,2000) for i in range(4)]
#所在城市平均薪水
salery_ave=[random.randint(3000,4000) for i in range(4)]
colors=["#007892","#ff427f","#fc8210","#ffd8a6"]
#进行绘图
bar=(
  Bar(init_opts = opts.InitOpts(width='600px',height='400px',bg_color=colors[-1]))
  .add_xaxis(name)
  .add_yaxis("salery",salery,stack="stack_one")
  .add_yaxis("cost",cost,stack="stack_one")
  .add_yaxis("salery_ave",salery_ave)
  .set_colors(colors)
  .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  .set_global_opts(title_opts=opts.TitleOpts(title="收入、消费及其城市平均收入情况"),
          legend_opts=opts.LegendOpts(type_="scroll", pos_right="right", orient="vertical")
          )
)
bar.render_notebook()

效果展示:

1.3.1 调整标题与图的位置

grid=Grid()
# 分别调整上下左右的位置,参数为像素值或百分比
grid.add(bar,grid_opts=opts.GridOpts(pos_top="30%",pos_bottom="10%",pos_left="10%",pos_right="10%"))
grid.render_notebook()

效果演示

1.4 绘制簇状图

#准备数据
name=["A","B","C","D"]
salery=[random.randint(3000,5000) for i in range(4)]
cost=[random.randint(1000,2000) for i in range(4)]
#所在城市平均薪水
salery_ave=[random.randint(3000,4000) for i in range(4)]
colors=["#007892","#ff427f","#fc8210","#ffd8a6"]
#进行绘图
bar=(
  Bar(init_opts = opts.InitOpts(width='600px',height='400px',bg_color=colors[-1]))
  .add_xaxis(name)
  .add_yaxis("salery",salery)
  .add_yaxis("salery_ave",salery_ave)
  .reversal_axis()
  .set_colors(colors)
  .set_series_opts(label_opts=opts.LabelOpts(position="right"))
  .set_global_opts(title_opts=opts.TitleOpts(title="收入、消费及其城市平均收入情况"),
          legend_opts=opts.LegendOpts(type_="scroll", pos_right="right", orient="vertical")
          )
)
bar.render_notebook()

效果图演示

1.5 数据量大时的显示方法

#准备数据
name=[chr(i) for i in range(65,85,1)]
salery=[random.randint(3000,5000) for i in range(20)]
#所在城市平均薪水
salery_ave=[random.randint(3000,4000) for i in range(20)]
colors=["#007892","#ff427f","#fc8210","#ffd8a6"]
#绘图 修改 orient为vertical,可将滑动按钮移动垂直方向
bar=(
  Bar(init_opts = opts.InitOpts(width='600px',height='400px',bg_color=colors[-1]))
  .add_xaxis(name)
  .add_yaxis("salery",salery)
  .add_yaxis("salery_ave",salery_ave)
  .set_colors(colors)
  .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  .set_global_opts(title_opts=opts.TitleOpts(title="收入、消费及其城市平均收入情况"),
          legend_opts=opts.LegendOpts(type_="scroll", pos_right="right", orient="vertical"),
          datazoom_opts=[opts.DataZoomOpts(type_="slider")]
          )
)
bar.render_notebook()

演示效果:

2.绘制散点图

2.1 普通散点图

import random
from pyecharts import options as opts
from pyecharts.charts import Scatter
from pyecharts.globals import ThemeType

#准备数据
name=["A","B","C","D"]
salery=[random.randint(3000,5000) for i in range(4)]
cost=[random.randint(1000,2000) for i in range(4)]
#所在城市平均薪水
salery_ave=[random.randint(3000,4000) for i in range(4)]
colors=["#007892","#ff427f","#fc8210","#ffd8a6"]
#进行绘图
scatter=(Scatter(init_opts = opts.InitOpts(width='600px',height='400px',theme=ThemeType.DARK))
    .add_xaxis(name)
    .add_yaxis("salery",salery)
    .add_yaxis("cost",cost)
    .set_global_opts(title_opts=opts.TitleOpts(title="收入与消费情况")))
scatter.render_notebook()

查看效果:

2.2 3D散点图绘制

import random
from pyecharts import options as opts
from pyecharts.charts import Scatter3D
from pyecharts.faker import Faker


#准备数据
data = [(random.randint(0,100),random.randint(0,100),random.randint(0,100)) for i in range(50)]
name=["长","宽","高"]
#绘图
scatter3D=Scatter3D(init_opts = opts.InitOpts(width='600px',height='400px')) #初始化
scatter3D.add(name,data,
     grid3d_opts=opts.Grid3DOpts(
     width=100, depth=100
    ))
scatter3D.set_global_opts(title_opts=opts.TitleOpts(title="散点图"),
             visualmap_opts=opts.VisualMapOpts(
             range_color=Faker.visual_color #颜色映射 
             ))
scatter3D.render_notebook()

效果图:

2.3 带涟漪的散点图

symbol的类型:
“pin”,“rect”,“roundRect”,“diamond”,“arrow”,“triangle”

import random
from pyecharts import options as opts
from pyecharts.charts import EffectScatter
from pyecharts.globals import ThemeType

#准备数据
name=["A","B","C","D"]
salery=[random.randint(3000,5000) for i in range(4)]
cost=[random.randint(1000,2000) for i in range(4)]
#所在城市平均薪水
salery_ave=[random.randint(3000,4000) for i in range(4)]
colors=["#007892","#ff427f","#fc8210","#ffd8a6"]
#进行绘图
scatter=(EffectScatter(init_opts = opts.InitOpts(width='600px',height='400px',theme=ThemeType.DARK))
    .add_xaxis(name)
    .add_yaxis("salery",salery,symbol="pin",symbol_size=20,symbol_rotate=180)
    .add_yaxis("cost",cost,symbol="rect",symbol_size=20)
    .set_global_opts(title_opts=opts.TitleOpts(title="收入与消费情况"),
            xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)), #添加网格
            yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True))
            )
    .set_series_opts(effect_opts=opts.EffectOpts(scale=3,period=2)) #调整涟漪的范围和周期
    )
scatter.render_notebook()

效果图如下:

加载全部内容

相关教程
猜你喜欢
用户评论