• 【Python】pyecharts 模块 ⑥ ( 绘制柱状图 | pyecharts 绘制柱状图步骤 | 柱状图 x 轴 / y 轴 翻转 | 柱状图数据标签位置设置 )



    pyecharts 画廊网站 : https://gallery.pyecharts.org/#/

    • 在该网站可查看官方示例




    一、pyecharts 绘制基础柱状图




    1、pyecharts 绘制柱状图步骤


    首先 , 导入 柱状图 Bar 类 , 该类定义在 pyecharts.charts 模块中 ;

    # 导入 pyecharts 模块中的 柱状图 Bar 类
    from pyecharts.charts import Bar
    
    • 1
    • 2

    然后 , 创建 柱状图 Bar 类型 实例对象 , 该对象代表了一个柱状图 ;

    # 创建柱状图对象
    bar = Bar()
    
    • 1
    • 2

    再后 , 设置该 柱状图的 x 轴 和 y 轴数据 ,

    • 调用 Bar#add_xaxis() 函数 , 设置 x 轴数据 , 实际数据放在 列表 中 , 作为参数传递给该函数 ;
    • 调用 Bar#add_yaxis() 函数 , 设置 y 轴数据 , 第一个参数是柱状图标题 , 第二个参数 是 列表类型的容器变量 , 表示 y 轴的数据 ;
    # 设置 x 轴数据
    bar.add_xaxis(["河北", "河南", "山东", "山西"])
    
    # 设置 y 轴数据
    bar.add_yaxis("GDP", [40391, 58887, 82875, 22870])
    
    • 1
    • 2
    • 3
    • 4
    • 5

    最后 , 调用 Bar#render() 函数 , 生成最终的柱状图 ;

    # 生成柱状图
    bar.render()
    
    • 1
    • 2

    在 该 源码 的 同级目录下 , 生成的 render.html 就是生成的 柱状图 ;

    在这里插入图片描述


    2、代码示例 - pyecharts 绘制柱状图


    代码示例 :

    """
    pyecharts 模块
    """
    
    # 导入 pyecharts 模块中的 柱状图 Bar 类
    from pyecharts.charts import Bar
    
    # 导入 配置 相关类
    from pyecharts.options import *
    
    # 创建柱状图对象
    bar = Bar()
    
    # 设置 x 轴数据
    bar.add_xaxis(["河北", "河南", "山东", "山西"])
    
    # 设置 y 轴数据
    bar.add_yaxis("GDP", [40391, 58887, 82875, 22870])
    
    # 生成柱状图
    bar.render()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    执行结果 :

    在这里插入图片描述

    生成的 render.html 如下 ( 仅做参考 - 没事别点开 ) :

    DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Awesome-pyechartstitle>
                    <script type="text/javascript" src="https://assets.pyecharts.org/assets/v5/echarts.min.js">script>
    
    head>
    <body >
        <div id="cefd15ca8045443380ca43cf4b559bc1" class="chart-container" style="width:900px; height:500px; ">div>
        <script>
            var chart_cefd15ca8045443380ca43cf4b559bc1 = echarts.init(
                document.getElementById('cefd15ca8045443380ca43cf4b559bc1'), 'white', {renderer: 'canvas'});
            var option_cefd15ca8045443380ca43cf4b559bc1 = {
        "animation": true,
        "animationThreshold": 2000,
        "animationDuration": 1000,
        "animationEasing": "cubicOut",
        "animationDelay": 0,
        "animationDurationUpdate": 300,
        "animationEasingUpdate": "cubicOut",
        "animationDelayUpdate": 0,
        "aria": {
            "enabled": false
        },
        "color": [
            "#5470c6",
            "#91cc75",
            "#fac858",
            "#ee6666",
            "#73c0de",
            "#3ba272",
            "#fc8452",
            "#9a60b4",
            "#ea7ccc"
        ],
        "series": [
            {
                "type": "bar",
                "name": "GDP",
                "legendHoverLink": true,
                "data": [
                    40391,
                    58887,
                    82875,
                    22870
                ],
                "realtimeSort": false,
                "showBackground": false,
                "stackStrategy": "samesign",
                "cursor": "pointer",
                "barMinHeight": 0,
                "barCategoryGap": "20%",
                "barGap": "30%",
                "large": false,
                "largeThreshold": 400,
                "seriesLayoutBy": "column",
                "datasetIndex": 0,
                "clip": true,
                "zlevel": 0,
                "z": 2,
                "label": {
                    "show": true,
                    "margin": 8
                }
            }
        ],
        "legend": [
            {
                "data": [
                    "GDP"
                ],
                "selected": {}
            }
        ],
        "tooltip": {
            "show": true,
            "trigger": "item",
            "triggerOn": "mousemove|click",
            "axisPointer": {
                "type": "line"
            },
            "showContent": true,
            "alwaysShowContent": false,
            "showDelay": 0,
            "hideDelay": 100,
            "enterable": false,
            "confine": false,
            "appendToBody": false,
            "transitionDuration": 0.4,
            "textStyle": {
                "fontSize": 14
            },
            "borderWidth": 0,
            "padding": 5,
            "order": "seriesAsc"
        },
        "xAxis": [
            {
                "show": true,
                "scale": false,
                "nameLocation": "end",
                "nameGap": 15,
                "gridIndex": 0,
                "inverse": false,
                "offset": 0,
                "splitNumber": 5,
                "minInterval": 0,
                "splitLine": {
                    "show": true,
                    "lineStyle": {
                        "show": true,
                        "width": 1,
                        "opacity": 1,
                        "curveness": 0,
                        "type": "solid"
                    }
                },
                "data": [
                    "\u6cb3\u5317",
                    "\u6cb3\u5357",
                    "\u5c71\u4e1c",
                    "\u5c71\u897f"
                ]
            }
        ],
        "yAxis": [
            {
                "show": true,
                "scale": false,
                "nameLocation": "end",
                "nameGap": 15,
                "gridIndex": 0,
                "inverse": false,
                "offset": 0,
                "splitNumber": 5,
                "minInterval": 0,
                "splitLine": {
                    "show": true,
                    "lineStyle": {
                        "show": true,
                        "width": 1,
                        "opacity": 1,
                        "curveness": 0,
                        "type": "solid"
                    }
                }
            }
        ]
    };
            chart_cefd15ca8045443380ca43cf4b559bc1.setOption(option_cefd15ca8045443380ca43cf4b559bc1);
        script>
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155

    柱状图效果如下 :

    在这里插入图片描述





    二、柱状图其它设置




    1、柱状图 x 轴 / y 轴 翻转


    调用 Bar#reversal_axis() 函数 , 可以翻转 柱状图 的 x 轴 和 y 轴 ;


    代码示例 :

    """
    pyecharts 模块
    """
    
    # 导入 pyecharts 模块中的 柱状图 Bar 类
    from pyecharts.charts import Bar
    
    # 导入 配置 相关类
    from pyecharts.options import *
    
    # 创建柱状图对象
    bar = Bar()
    
    # 设置 x 轴数据
    bar.add_xaxis(["河北", "河南", "山东", "山西"])
    
    # 设置 y 轴数据
    bar.add_yaxis("GDP", [40391, 58887, 82875, 22870])
    
    # 翻转 x 轴 / y 轴
    bar.reversal_axis()
    
    # 生成柱状图
    bar.render()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    打开运行后生成的 render.html 网页 , 效果如下 :

    在这里插入图片描述


    2、柱状图数据标签位置设置


    上面的柱状图的 数值标签 都在柱子 的中心位置显示 , 这是默认显示位置 ;

    在这里插入图片描述


    如果我们想要让 数值数据 显示在最右侧 , 在添加 y 轴数据时 , 为其设置一个 LabelOpts 参数 ;

    # 设置 y 轴数据
    bar.add_yaxis("GDP", [40391, 58887, 82875, 22870],
                  label_opts=LabelOpts(position="right"))
    
    • 1
    • 2
    • 3

    完整代码示例 :

    """
    pyecharts 模块
    """
    
    # 导入 pyecharts 模块中的 柱状图 Bar 类
    from pyecharts.charts import Bar
    
    # 导入 配置 相关类
    from pyecharts.options import *
    
    # 创建柱状图对象
    bar = Bar()
    
    # 设置 x 轴数据
    bar.add_xaxis(["河北", "河南", "山东", "山西"])
    
    # 设置 y 轴数据
    bar.add_yaxis("GDP", [40391, 58887, 82875, 22870],
                  label_opts=LabelOpts(position="right"))
    
    # 翻转 x 轴 / y 轴
    bar.reversal_axis()
    
    # 生成柱状图
    bar.render()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    render.html 网页显示效果 : 数值都在柱状数据的右侧显示 ;

    在这里插入图片描述

  • 相关阅读:
    【送书活动1】强势挑战Java,Kotlin杀回TIOBE榜单Top 20!
    vscode快捷键使用总结
    mysql同时使用order by排序和limit分页数据重复问题
    echarts地图的散点,在时间轴上,为什么点击中国地图的江苏省,中国地图上的散点会散开
    (已解决) MINet 进行测试时报错如下 raise NotImplementedError
    8种结构型设计模式对比
    记录一次由cnpm引发的问题
    Linux C/C++下收集指定域名的子域名信息(类似dnsmap实现)
    数据结构顺序表之单链表
    ROS 语音交互(三) tts
  • 原文地址:https://blog.csdn.net/han1202012/article/details/131883321