DDR爱好者之家 Design By 杰米
近期,又有接触到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()
效果图如下:
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]