【Python】pyecharts 模块 ④ ( pyecharts 模块常用配置 | 全局配置和系列配置 | 全局配置详细描述 | 模块配置提示 | 模块配置文档 | 代码示例 )

文章目录

  • 一、pyecharts 模块配置
    • 1、pyecharts 模块常用配置
    • 2、pyecharts 模块全局配置和系列配置
    • 3、pyecharts 模块全局配置详细描述
    • 4、pyecharts 模块配置提示
    • 5、pyecharts 模块配置文档
    • 6、代码示例 – pyecharts 模块全局配置

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

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




一、pyecharts 模块配置



1、pyecharts 模块常用配置


pyecharts 模块 有很多 配置选项 , 常用的配置项有如下几种 :

  • 初始化配置项 ( InitOpts )
  • 标题配置项 ( TitleOpts )
  • 图例配置项
  • 工具箱配置项 ( ToolboxOpts )
  • 视觉映射配置项 ( VisualMapOpts )
  • 提示框配置项 ( TooltipOpts )
  • 区域缩放配置项 ( DataZoomOpts )

2、pyecharts 模块全局配置和系列配置


Pyecharts 提供了一些全局配置和系列配置选项 , 用于控制图表的外观和行为 ;

  • 全局配置 : 通过 set_global_opts() 方法进行设置,可以修改图表的默认配置,例如主题、自动调整大小、宽度和高度等。
  • 系列配置 : 用于控制每个系列(series)的图表样式和数据,例如线条样式、柱状图颜色、标签格式等。系列配置通过 set_series_opts() 方法进行设置,可以选择多种系列类型,如线图、柱状图、散点图等。

3、pyecharts 模块全局配置详细描述


下图是一个 pyecharts 图表的常见全局配置 :

常见的 Pyecharts 模块全局配置选项:

  • InitOpts(初始化配置项)

    • theme:主题设置。
    • auto_resize:是否自动调整图表大小以适应内容。
    • width:图表的宽度。
    • height:图表的高度。
  • TitleOpts(标题配置项)

    • text:标题文本。
    • subtext:副标题文本。
    • left:标题距离容器左侧的距离。
    • top:标题距离容器顶部的距离。
    • textStyle:标题文本的样式设置。
  • TooltipOpts(提示框配置项)

    • trigger:提示框触发的条件,可选:‘mousemove’、‘click’、‘mousemove|click’、‘none’。
    • axisPointerType:指示器类型,可选:‘line’、‘shadow’、‘none’、‘cross’。
    • backgroundColor:提示框浮层的背景颜色。
    • borderColor:提示框浮层的边框颜色。
    • borderWidth:提示框浮层的边框宽。
    • isShow:是否显示提示框组件,包括提示框浮层和 axisPointer。
  • AxisOpts(坐标轴配置项)

    • type:坐标轴类型,如 ‘value’、‘category’、‘time’ 等。
    • name:坐标轴名称。
    • nameTextStyle:坐标轴名称的样式设置。
    • data:坐标轴数据。
    • splitNumber:分段数。
    • min:最小值。
    • max:最大值。
    • splitLine:分段线。
    • nameGap:名称与坐标轴线之间的距离。
    • nameTextStyle:坐标轴名称的样式设置。
  • VisualMapOpts(视觉映射配置设置)

    • isShow:是否显示视觉映射组件。
    • orient:视觉映射组件的方向。
    • inRange:视觉映射组件的取值范围。
    • itemStyle:视觉映射组件的样式设置。

4、pyecharts 模块配置提示


在 set_global_opts 函数中 , 点击 Ctrl + P 可以看到可配置的全局配置项 ;

在具体的配置项中 , 在配置项构造函数中 , 点击 Ctrl + P 可以查看具体可以配置哪些属性 ;


5、pyecharts 模块配置文档


pyecharts 模块全局配置项文档 : https://pyecharts.org/#/zh-cn/global_options

在上述页面中 , 可以找到全局配置项文档 ;


6、代码示例 – pyecharts 模块全局配置


代码示例 :

"""
pyecharts 模块
"""

# 导入 pyecharts 模块中的 折线图 Line 对象
from pyecharts.charts import Line

# 全局配置中的标题配置, 图例配置, 工具箱配置, 视觉映射配置
from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts

# 创建 折线图 对象
line = Line()

# 设置 x 轴数据
line.add_xaxis(["西城", "东城", "海淀", "朝阳", "昌平"])

# 设置 y 轴数据
line.add_yaxis("清北录取人数", [105, 28, 215, 16, 1])

# 设置全局配置项

# 配置标题
# 调用 set_global_opts 方法, 传入的参数使用关键字传参
line.set_global_opts(
    # pos_left="center" 表示标题水平方向居中对齐
    # pos_bottom="1%" 表示距离底部 1%
    title_opts=TitleOpts(title="清北录取人数", pos_left="center", pos_bottom="1%"),

    # 图例配置
    legend_opts=LegendOpts(is_show=True),

    # 工具箱配置
    toolbox_opts=ToolboxOpts(is_show=True),

    # 视觉映射配置
    visualmap_opts=VisualMapOpts(is_show=True)
)

# 生成图表
line.render()

运行结果 : 运行上述代码 , 生成 render.html 网页 ;

render.html 网页内容 :

<!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="b74fc495f77544f5a15f5a3814366245" class="chart-container" style="width:900px; height:500px; "></div>
    <script>
        var chart_b74fc495f77544f5a15f5a3814366245 = echarts.init(
            document.getElementById('b74fc495f77544f5a15f5a3814366245'), 'white', {renderer: 'canvas'});
        var option_b74fc495f77544f5a15f5a3814366245 = {
    "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": "line",
            "name": "\u6e05\u5317\u5f55\u53d6\u4eba\u6570",
            "connectNulls": false,
            "xAxisIndex": 0,
            "symbolSize": 4,
            "showSymbol": true,
            "smooth": false,
            "clip": true,
            "step": false,
            "data": [
                [
                    "\u897f\u57ce",
                    105
                ],
                [
                    "\u4e1c\u57ce",
                    28
                ],
                [
                    "\u6d77\u6dc0",
                    215
                ],
                [
                    "\u671d\u9633",
                    16
                ],
                [
                    "\u660c\u5e73",
                    1
                ]
            ],
            "hoverAnimation": true,
            "label": {
                "show": true,
                "margin": 8
            },
            "logBase": 10,
            "seriesLayoutBy": "column",
            "lineStyle": {
                "show": true,
                "width": 1,
                "opacity": 1,
                "curveness": 0,
                "type": "solid"
            },
            "areaStyle": {
                "opacity": 0
            },
            "zlevel": 0,
            "z": 0
        }
    ],
    "legend": [
        {
            "data": [
                "\u6e05\u5317\u5f55\u53d6\u4eba\u6570"
            ],
            "selected": {},
            "show": true,
            "padding": 5,
            "itemGap": 10,
            "itemWidth": 25,
            "itemHeight": 14,
            "backgroundColor": "transparent",
            "borderColor": "#ccc",
            "borderWidth": 1,
            "borderRadius": 0,
            "pageButtonItemGap": 5,
            "pageButtonPosition": "end",
            "pageFormatter": "{current}/{total}",
            "pageIconColor": "#2f4554",
            "pageIconInactiveColor": "#aaa",
            "pageIconSize": 15,
            "animationDurationUpdate": 800,
            "selector": false,
            "selectorPosition": "auto",
            "selectorItemGap": 7,
            "selectorButtonGap": 10
        }
    ],
    "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": [
                "\u897f\u57ce",
                "\u4e1c\u57ce",
                "\u6d77\u6dc0",
                "\u671d\u9633",
                "\u660c\u5e73"
            ]
        }
    ],
    "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"
                }
            }
        }
    ],
    "title": [
        {
            "show": true,
            "text": "\u6e05\u5317\u5f55\u53d6\u4eba\u6570",
            "target": "blank",
            "subtarget": "blank",
            "left": "center",
            "bottom": "1%",
            "padding": 5,
            "itemGap": 10,
            "textAlign": "auto",
            "textVerticalAlign": "auto",
            "triggerEvent": false
        }
    ],
    "toolbox": {
        "show": true,
        "orient": "horizontal",
        "itemSize": 15,
        "itemGap": 10,
        "left": "80%",
        "feature": {
            "saveAsImage": {
                "type": "png",
                "backgroundColor": "auto",
                "connectedBackgroundColor": "#fff",
                "show": true,
                "title": "\u4fdd\u5b58\u4e3a\u56fe\u7247",
                "pixelRatio": 1
            },
            "restore": {
                "show": true,
                "title": "\u8fd8\u539f"
            },
            "dataView": {
                "show": true,
                "title": "\u6570\u636e\u89c6\u56fe",
                "readOnly": false,
                "lang": [
                    "\u6570\u636e\u89c6\u56fe",
                    "\u5173\u95ed",
                    "\u5237\u65b0"
                ],
                "backgroundColor": "#fff",
                "textareaColor": "#fff",
                "textareaBorderColor": "#333",
                "textColor": "#000",
                "buttonColor": "#c23531",
                "buttonTextColor": "#fff"
            },
            "dataZoom": {
                "show": true,
                "title": {
                    "zoom": "\u533a\u57df\u7f29\u653e",
                    "back": "\u533a\u57df\u7f29\u653e\u8fd8\u539f"
                },
                "icon": {},
                "filterMode": "filter"
            },
            "magicType": {
                "show": true,
                "type": [
                    "line",
                    "bar",
                    "stack",
                    "tiled"
                ],
                "title": {
                    "line": "\u5207\u6362\u4e3a\u6298\u7ebf\u56fe",
                    "bar": "\u5207\u6362\u4e3a\u67f1\u72b6\u56fe",
                    "stack": "\u5207\u6362\u4e3a\u5806\u53e0",
                    "tiled": "\u5207\u6362\u4e3a\u5e73\u94fa"
                },
                "icon": {}
            }
        }
    },
    "visualMap": {
        "show": true,
        "type": "continuous",
        "min": 0,
        "max": 100,
        "inRange": {
            "color": [
                "#50a3ba",
                "#eac763",
                "#d94e5d"
            ]
        },
        "calculable": true,
        "inverse": false,
        "splitNumber": 5,
        "hoverLink": true,
        "orient": "vertical",
        "padding": 5,
        "showLabel": true,
        "itemWidth": 20,
        "itemHeight": 140,
        "borderWidth": 0
    }
};
        chart_b74fc495f77544f5a15f5a3814366245.setOption(option_b74fc495f77544f5a15f5a3814366245);
    </script>
</body>
</html>

使用 Chrome 浏览器打开网页 , 效果如下 :

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年12月15日
下一篇 2023年12月15日

相关推荐