Loading...
墨滴

Wyl

2021/04/20  阅读:141  主题:橙心

可视化 | 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 = [859095759298]
y_value2 = [958885968776]


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 = [859095759298]
y_value2 = [958885968776]


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 = [859095759298]
y_value2 = [958885968776]


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 = [958885968776]


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 = [859095759298]
y_value2 = [958885968776]


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 = [859095759298]
y_value2 = [958885968776]


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 = [859095759298]
y_value2 = [958885968776]


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 = [859095759298]
y_value2 = [958885968776]


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 = [859095759298]
y_value2 = [958885968776]


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 = [859095759298]
y_value2 = [958885968776]


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 = [859095759298]
y_value2 = [958885968776]


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 = [859095759298]
y_value1plus = [101010101010#学生1三好学生每门课程加10分
y_value2 = [958885968776]


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 = [859095759298]
classrank = [30251060155]

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

知乎与公号:乌龙PySta(ID:wylcfy2014)
不定期推送:Python+Stata | 文本分析+机器学习 | 财务+会计

参考资料

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

Wyl

2021/04/20  阅读:141  主题:橙心

作者介绍

Wyl

厦门大学