python 柱状图_python数据分析之——pyecharts柱状图进阶篇,带你玩转柱状图

7d9a5390fb42afb7dabf020ae1721e5c.png

上一篇,我们讲解了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()
c5e593a393935394f5d0094c5c9a0ce1.png

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()

1753c67087bb7eb2942544bfb454ec06.png
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()
6641caf330fcc6ee321224215e365d86.png

extend_axis:增加了以温度为刻度的纵坐标轴

add_yaxis:yaxis_index=0表示该数据用第一个坐标轴,yaxis_index=1表示该数据用第二个坐标轴

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()
1cf8747d53843a424056c2f815e9fc59.png

只需更改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()

c3df2968358d16d64622c6cda879c2b5.gif

animation_delay设置延迟时间

animation_easing设置动画效果

-完-

推荐阅读:

python数据分析——pyecharts柱状图全解(小白必看)

python数据分析之环境搭建——小白也能看懂的Jupyter Notebook安装与使用教程
Python深度分析出高考最难的年份

5996e1e35ab7dac3f47141ba72269730.png

长按回复下方【关键词】,获取优质资源:

回复关键词【 福利1】,立即获取python数据分析教学视频回复关键词【 福利2】,立即获取python学习电子书
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值