可视化 | pyecharts之柱状图常用配置篇

前言

pyecharts的可视化大法,让人爱不释手。柱状图是我们最为常用的可视化统计图,本篇主要介绍了pyecharts的绘制柱状图的常用配置,主要包括以下内容:

  1. 基础柱状图
  2. 隐藏图例标签数字
  3. 坐标轴名称命名
  4. 旋转X轴标签
  5. 增加标记线或者标记点
  6. 柱子宽度设置
  7. 不同系列间柱间距离
  8. 自定义柱状颜色
  9. 柱状堆叠图
  10. 在柱状图中同时绘制折线图

实例详解

基础柱状图

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1) # y轴设置
    .add_yaxis("学生B", y_value2) # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
    )

bar.render_notebook()

隐藏图例标签数字

在系列配置项中set_series_opts()的标签设置

label_opts=opts.LabelOpts(is_show=False),False为隐藏数字

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1) # y轴设置
    .add_yaxis("学生B", y_value2) # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    )

bar.render_notebook()

坐标轴名称命名

全局配置项中,yaxis_opts=opts.AxisOpts(name)以及xaxis_opts=opts.AxisOpts(name)参数设置

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1, stack = "stack1") # y轴设置
    .add_yaxis("学生B", y_value2) # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="坐标轴命名"),
                    yaxis_opts=opts.AxisOpts(name="课程成绩"),
                    xaxis_opts=opts.AxisOpts(name="课程类别"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) 
    )

bar.render_notebook()

旋转x轴标签

全局配置项中,xaxis_opts=opts.AxisOpts(totate)参数设置,rotate = -15,垂直x轴标签逆时针旋转15度

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["很长很长的高微","很长很长的高管","很长很长的高计","很长很长的会计","很长很长的金融","很长很长的计算机"]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1) # y轴设置
    .add_yaxis("学生B", y_value2) # y轴设置
    .set_global_opts(
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
        title_opts=opts.TitleOpts(title="旋转x轴标签", subtitle="我是副标题"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) 
    )

bar.render_notebook()

旋转坐标轴

系列配置项中,reversal_axis(),label_opts=opts.LabelOpts(position=“right”)

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1, stack = "stack1") # y轴设置
    .add_yaxis("学生B", y_value2) # y轴设置
    .reversal_axis()
    .set_global_opts(title_opts=opts.TitleOpts(title="旋转坐标轴"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False,position="right")) 
    )

bar.render_notebook()

增加标记线或者标记点

一、指定值的标记线

在系列配置项中set_series_opts()的markline_opts=opts.MarkLineOpts()

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1) # y轴设置
    .add_yaxis("学生B", y_value2) # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="增加标记线", subtitle="我是副标题"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False),
                    markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(y=75, name="yAxis=75")])) # 75分合格线
    )

bar.render_notebook()

二、平均值、最小值、最大值的标记线

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1) # y轴设置
    #.add_yaxis("学生B", y_value2) # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
    .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()

三、增加标记点

从“线型”替换成“点型”,markline_opts参数设置变为markpoint_opts

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1) # y轴设置
    .add_yaxis("学生B", y_value2) # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
    .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()

柱子宽度设置

.add_yaxis(category_gap=“80%”)参数设置,值越大表明柱子间的间距越大,柱子宽度越小

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1, category_gap="50%") # y轴设置
    .add_yaxis("学生B", y_value2, category_gap="50%") # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="柱子宽度设置", subtitle="我是副标题"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) 
    )

bar.render_notebook()

不同系列柱间距离

.add_yaxis(gap=“0%”)参数设置,,值越小表明不同系列之间的柱间距离越小

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1, gap="0%") # y轴设置
    .add_yaxis("学生B", y_value2, gap="0%") # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False),
                    markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(y=75, name="yAxis=75")])) # 75分合格线
    )

bar.render_notebook()

自定义柱状颜色

.add_yaxis()参数设置,itemstyle_opts=opts.ItemStyleOpts(color)

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1, itemstyle_opts=opts.ItemStyleOpts(color="gray")) # y轴设置
    .add_yaxis("学生B", y_value2, itemstyle_opts=opts.ItemStyleOpts(color="black")) # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    )

bar.render_notebook()

柱状堆叠

.add_yaxis()参数设置,stack参数设置

from pyecharts import options as opts
from pyecharts.charts import *

x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
y_value1plus = [10, 10, 10, 10, 10, 10] #学生1三好学生每门课程加10分
y_value2 = [95, 88, 85, 96, 87, 76]


bar = (
    Bar()
    .add_xaxis(x_index)
    .add_yaxis("学生A", y_value1, stack = "stack1") # y轴设置
    .add_yaxis("加分", y_value1plus, stack = "stack1") # y轴设置
    .add_yaxis("学生B", y_value2) # y轴设置
    .set_global_opts(title_opts=opts.TitleOpts(title="柱状堆叠", subtitle="我是副标题"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) 
    )

bar.render_notebook()

在柱状图中同时绘制折线图

import pyecharts.options as opts
from pyecharts.charts import *


x_index = ["高微","高管","高计","会计","金融","计算机"]
y_value1 = [85, 90, 95, 75, 92, 98]
classrank = [30, 25, 10, 60, 15, 5]

bar = (
    Bar(init_opts=opts.InitOpts(width="800px", height="400px"))
    .add_xaxis(xaxis_data=x_index)
    .add_yaxis(
        series_name="课程成绩",
        y_axis=y_value1,
        category_gap="50%",
        label_opts=opts.LabelOpts(is_show=False)
        )

    .extend_axis(   # 第二坐标轴
        yaxis=opts.AxisOpts(
            name="课程排名",
            type_="value",
            min_=0,
            max_=100,
            interval=20,
            axislabel_opts=opts.LabelOpts(formatter="{value} %") # 设置坐标轴格式
                            )
                )
    .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_=100,
            interval=20,
            axislabel_opts=opts.LabelOpts(formatter="{value} 分"), # 设置坐标轴格式
            axistick_opts=opts.AxisTickOpts(is_show=True),
            splitline_opts=opts.SplitLineOpts(is_show=True),
        ),
    )
)

line = (
    Line()
    .add_xaxis(xaxis_data=x_index)
    .add_yaxis(
        series_name="课程成绩",
        yaxis_index=1,
        y_axis=classrank,
        itemstyle_opts=opts.ItemStyleOpts(color="blue"),
        label_opts=opts.LabelOpts(is_show=False),
        z=2 # 使折线图显示在柱状图上面
    )
)

bar.overlap(line).render_notebook()

参考资料

[1]https://gallery.pyecharts.org/#/Bar/README

  • 11
    点赞
  • 106
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AccountingCoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值