上一篇,我们讲解了pyecharts柱状图的常见类型,能够满足基本的工作学习需求,但是pyecharts的功能远不止于此,下面我将继续给大家介绍柱状图进阶展示形式,让你画出更酷炫的柱状图。
1.数据堆叠柱状图
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, stack="stack1")
.add_yaxis("l3", l3, stack="stack1")
.set_global_opts(title_opts=opts.TitleOpts(title="数据堆叠"))
)
bar.render_notebook()
2.柱状图和折线图合并
import pyecharts.options as opts
from pyecharts.charts import Bar, Line
x_data = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
bar = (
Bar(init_opts=opts.InitOpts(width="1000px", height="500px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="降水量",
yaxis_data=[
2.6,
5.9,
9.0,
26.4,
28.7,
70.7,
175.6,
182.2,
48.7,
18.8,
6.0,
2.3,
],
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="水量",
type_="value",
min_=0,
max_=250,
interval=50,
axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
)
line = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="降水量",
yaxis_index=0,
y_axis=[2.6,5.9,9.0,26.4,28.7,70.7,175.6,182.2,48.7,18.8,6.0,2.3,],
label_opts=opts.LabelOpts(is_show=False),
)
)
bar.overlap(line).render_notebook()
yaxis_opts=opts.AxisOpts()中可以设置纵坐标起止范围和间隔
3.双纵坐标柱状图
如果想把温度和降雨量画在同一个柱状图内,一个纵坐标就不够用了
import pyecharts.options as opts
from pyecharts.charts import Bar, Line
x_data = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
bar = (
Bar(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="蒸发量",
yaxis_index=0,
yaxis_data=[
2.0,
4.9,
7.0,
23.2,
25.6,
76.7,
135.6,
162.2,
32.6,
20.0,
6.4,
3.3,
],
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
series_name="平均温度",
yaxis_index=1,
yaxis_data=[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
label_opts=opts.LabelOpts(is_show=False),
)
.extend_axis(
yaxis=opts.AxisOpts(
name="温度",
type_="value",
min_=0,
max_=25,
interval=5,
axislabel_opts=opts.LabelOpts(formatter="{value} °C"),
)
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="水量",
type_="value",
min_=0,
max_=250,
interval=50,
axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
)
bar.render_notebook()
extend_axis:增加了以温度为刻度的纵坐标轴
4.为柱状图添加背景图片
觉得背景太单调?那就来自定义一下背景图片吧
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
l2=[100,200,300,400,500,400,300]
l3=[300,400,500,400,300,200,100]
bar = (
Bar(
init_opts=opts.InitOpts(
bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat"}
)
)
.add_xaxis(Faker.choose())
.add_yaxis("商家A", l2)
.add_yaxis("商家B", l3)
.set_global_opts(
title_opts=opts.TitleOpts(
title="Bar-背景图基本示例",
subtitle="我是副标题",
title_textstyle_opts=opts.TextStyleOpts(color="white"),
)
)
)
bar.add_js_funcs(
"""
var img = new Image(); img.src = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598692410282&di=3a4c3f5770826c30089dd3975357bcd7&imgtype=0&src=http%3A%2F%2Fdimg01.c-ctrip.com%2Fimages%2Ffd%2Ftg%2Fg6%2FM00%2F4A%2F70%2FCggYslbXsPaABfvoAAE_7azp5ow674_R_1024_10000_Q90.jpg';
"""
)
bar.render_notebook()
只需更改img.src中图片url地址即可更换背景
5.为柱状图添加动画
简单的出场方式已经不能满足我的需要了,我需要酷炫一点的
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
l1=[100,200,300,400,500,400,300]
l2=[300,400,500,400,300,200,100]
bar = (
Bar(
init_opts=opts.InitOpts(
animation_opts=opts.AnimationOpts(
animation_delay=1000, animation_easing="bounceIn"
)
)
)
.add_xaxis(Faker.choose())
.add_yaxis("商家A", l1)
.add_yaxis("商家B", l2)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-动画配置基本示例", subtitle="我是副标题"))
)
bar.render_notebook()
animation_delay设置延迟时间
animation_easing设置动画效果
-完-
推荐阅读:
python数据分析——pyecharts柱状图全解(小白必看)
python数据分析之环境搭建——小白也能看懂的Jupyter Notebook安装与使用教程
Python深度分析出高考最难的年份
长按回复下方【关键词】,获取优质资源:
回复关键词【 福利1】,立即获取python数据分析教学视频回复关键词【 福利2】,立即获取python学习电子书