一、pyecharts简介
pyecharts主要基于Web浏览器进行显示,绘制的图形比较多,包括折线图、柱状图、饼图、漏斗图 地图和极坐标图等。使用pyecharts绘图代码量很少,但绘制的图形比较美观。
pyecharts 分为 v0.5.X 和 v1 两个大版本,v0.5.X 和 v1 间不兼容,v1 是一个全新的版本 v0.5.X支持 Python2.7,3.4+。
经开发团队决定,0.5.x 版本将不再进行维护,0.5.x 版本代码位于 05x 分支 ,v1仅支持 Python3.6+,新版本系列将从 v1.0.0 开始。
本文所讲主要基于 pyecharts 1.7.1 版本进行展示 安装命令为:
pip install pyecharts==1.7.1
二、pyecharts柱状图/条形图全解
1.基本柱状图/条形图
from pyecharts import options as opts from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日'] l2=[100,200,300,400,500,400,300]bar = ( Bar() .add_xaxis(l1) .add_yaxis("基本柱状图", l2) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题")) )bar.render_notebook()
参数介绍:
add_xaxis:添加横坐标,需传入列表 add_yaxis:添加纵坐标,需传入列表,且列表元素为数值
2.添加坐标轴名称
from pyecharts import options as opts from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日'] l2=[100,200,300,400,500,400,300] bar = ( Bar() .add_xaxis(l1) .add_yaxis("基本柱状图", l2) .set_global_opts( title_opts=opts.TitleOpts(title="Bar-基本示例"), yaxis_opts=opts.AxisOpts(name="人流量"), xaxis_opts=opts.AxisOpts(name="星期"),) ) bar.render_notebook()
3.多个纵坐标的柱状图/条形图
from pyecharts import options as opts from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日'] l2=[100,200,300,400,500,400,300]l3=[300,400,500,400,300,200,100] bar = ( Bar() .add_xaxis(l1) .add_yaxis("l2", l2) .add_yaxis("l3", l3) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"), toolbox_opts=opts.BrushOpts(),) ) bar.render_notebook()
opts.BrushOpts()为圈选工具,如图形右上角所示
4.设置柱状图间隔和颜色
from pyecharts import options as opts from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日'] l2=[100,200,300,400,500,400,300] bar = ( Bar() .add_xaxis(l1) .add_yaxis("l2",l2,category_gap=0, color='#FFFF00') .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题")) ) bar.render_notebook()
category_gap:设置间隔
color:设置柱状图颜色
5.横向柱状图
from pyecharts import options as opts from pyecharts.charts import Bar
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日'] l2=[100,200,300,400,500,400,300]l3=[300,400,500,400,300,200,100] bar = ( Bar() .add_xaxis(l1) .add_yaxis("l2", l2) .add_yaxis("l3", l3) .reversal_axis() .set_series_opts(label_opts=opts.LabelOpts(position="right")) .set_global_opts(title_opts=opts.TitleOpts(title="横向柱状图")) ) bar.render_notebook()
reversal_axis将图形反转
position="right"表示将数值在图形右侧显示,同理left、center分别表示左侧和中间
6.显示最大值、最小值和平均值
a.标记线
from pyecharts import options as opts from pyecharts.charts import Barimport random
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日'] l2=[100,200,300,400,500,400,300] bar = ( Bar() .add_xaxis(l1) .add_yaxis("l2", l2) .set_global_opts(title_opts=opts.TitleOpts(title="标记线柱状图")) .set_series_opts( label_opts=opts.LabelOpts(is_show=False), markline_opts=opts.MarkLineOpts( data=[ opts.MarkLineItem(type_="min", name="最小值"), opts.MarkLineItem(type_="max", name="最大值"), opts.MarkLineItem(type_="average", name="平均值"), ] ), ) ) bar.render_notebook()
b.标记点
from pyecharts import options as opts from pyecharts.charts import Barimport random
l1=['星期一','星期二','星期三','星期四','星期五','星期七','星期日'] l2=[100,200,300,400,500,400,300] bar = ( Bar() .add_xaxis(l1) .add_yaxis("l2", l2) .set_global_opts(title_opts=opts.TitleOpts(title="标记线柱状图")) .set_series_opts( label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts( data=[ opts.MarkPointItem(type_="min", name="最小值"), opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="average", name="平均值"), ] ), ) )bar.render_notebook()
7.旋转x轴坐标
from pyecharts import options as opts from pyecharts.charts im
port Barimport random
l1=['很长很长很长很长很长的坐标轴{}'.format(i) for i in range(10)] l2=[random.choice(range(10,100,10)) for i in range(10)] bar = ( Bar() .add_xaxis(l1) .add_yaxis("l2", l2) .set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)), title_opts=opts.TitleOpts(title="Bar-旋转X轴标签", subtitle="解决标签名字过长的问题")) ) bar.render_notebook()
rotate=-15表示将坐标轴逆时针旋转15度
8.横坐标缩放
a.整体缩放(type_=“inside”)
```python from pyecharts import options as opts from pyecharts.charts import Barimport random
l1=['{}日'.format(i) for i in range(1,31)] l2=[random.choice(range(100,3100,100)) for i in range(1,31)] bar = ( Bar() .add_xaxis(l1) .add_yaxis("l2", l2) .set_global_opts(title_opts=opts.TitleOpts(title="区域缩放柱状图"), datazoom_opts=opts.DataZoomOpts(type_="inside")) ) bar.render_notebook()
b.左右滑动缩放
```python
from pyecharts import options as opts
from pyecharts.charts import Barimport random
l1=['{}日'.format(i) for i in range(1,31)]
l2=[random.choice(range(100,3100,100)) for i in range(1,31)]bar = (
Bar()
.add_xaxis(l1)
.add_yaxis("l2", l2)
.set_global_opts(title_opts=opts.TitleOpts(title="区域缩放柱状图"),
datazoom_opts=opts.DataZoomOpts(type_="slider"))
)
bar.render_notebook()
本次主要介绍了pyecharts柱状图的常见形式,后续会出来pyecharts柱状图的高阶用法,敬请关注!
本文地址:https://blog.csdn.net/Androidyuexia/article/details/108247393
希望与广大网友互动??
点此进行留言吧!