[Python] pyecharts module ⑥ ( draw histogram | pyecharts draw histogram steps | histogram x-axis / y-axis flip | histogram data label position setting )


pyecharts gallery website: https://gallery.pyecharts.org/#/

  • Check out the official example at this site




1. pyecharts draws basic histograms




1. pyecharts draw histogram steps


First, import the histogram Bar class, which is defined in the pyecharts.charts module;

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

Then, create a histogram Bar type instance object, which represents a histogram;

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

Then, set the x-axis and y-axis data of the histogram,

  • Call the Bar#add_xaxis() function, set the x-axis data, the actual data is placed in the list, and passed to the function as a parameter;
  • Call the Bar#add_yaxis() function to set the y-axis data, the first parameter is the column chart title, and the second parameter is a container variable of list type, representing the data of the y-axis;
# 设置 x 轴数据
bar.add_xaxis(["河北", "河南", "山东", "山西"])

# 设置 y 轴数据
bar.add_yaxis("GDP", [40391, 58887, 82875, 22870])

Finally, call the Bar#render() function to generate the final histogram;

# 生成柱状图
bar.render()

In the same level directory of the source code, the generated render.html is the generated histogram;

insert image description here


2. Code example - pyecharts draw histogram


Code example:

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

Results of the :

insert image description here

The generated render.html is as follows (just for reference - don't click to open if you have nothing to do):

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Awesome-pyecharts</title>
                <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>

The effect of the histogram is as follows:

insert image description here





2. Other settings of the histogram




1. Histogram x-axis/y-axis flip


Call the Bar#reversal_axis() function to reverse the x-axis and y-axis of the histogram;


Code example:

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

Open the render.html web page generated after running, the effect is as follows:

insert image description here


2. Histogram data label position setting


The numerical labels of the above histogram are all displayed in the center of the column, which is the default display position;

insert image description here


If we want the numerical data to be displayed on the far right, set a LabelOpts parameter for it when adding the y-axis data;

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

Complete code example:

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


render.html Web page display effect: the values ​​are displayed on the right side of the columnar data;

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/131883321