目录
一、安装pyecharts库
二、pyecharts柱状图/条形图全解
1.基本柱状图/条形图
2.添加坐标轴名称
3.多个纵坐标的柱状图/条形图
4.设置柱状图间隔和颜色
5.横向柱状图
安装步骤: 1)打开cmd(Win+R); 2)输入:pip install pyecharts,回车
代码如下:
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:添加纵坐标,需传入列表,切列表元素为数值
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()
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()为圈选工具,如图形右上角所示
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:设置柱状图颜色
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分别表示左侧和中间