You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自定义Python中Excel::Writer::XLSX::Stock图表的顶/中/底标记样式?

修改Excel::Writer::XLSX股票图表标记样式的实现方法

要实现顶部标记红菱形、中部红圆形、底部绿方形的自定义样式,你可以直接在每个系列的marker配置中指定对应的形状和颜色,替换默认的automatic类型即可。

具体修改方案

  1. 提前为三个系列(低值、均值、高值)定义好对应的标记样式
  2. 在循环中根据当前列的位置匹配对应的样式

修改后的代码如下:

chart = workbook.add_chart({"type": "stock"})
column_index_range = range(
    min_mean_max_dataframe_column_count - 2,
    min_mean_max_dataframe_column_count + 1,
)

# 定义三个系列的标记样式:底部(低值)、中部(均值)、顶部(高值)
marker_styles = [
    {"type": "square", "color": "green", "size": 4},    # 绿色方形
    {"type": "circle", "color": "red", "size": 4},      # 红色圆形
    {"type": "diamond", "color": "red", "size": 4}      # 红色菱形
]

for idx, column_index in enumerate(column_index_range):
    series = {
        "name": [sheet_name, starting_row, column_index],
        "categories": [
            sheet_name,
            starting_row + 1,
            0,
            starting_row + 11,
            0,
        ],  # 排除平均值列
        "values": [
            sheet_name,
            starting_row + 1,
            column_index,
            starting_row + 11,
            column_index,
        ],
        # 替换默认automatic,使用对应索引的标记样式
        "marker": marker_styles[idx],
    }
    chart.set_high_low_lines(
        {
            "line": {"width": 2},
        },
    )
    chart.add_series(series)

关键细节说明

  • marker的type参数支持的形状包括:square(方形)、diamond(菱形)、circle(圆形)、triangle(三角形)、star(星形)等
  • 颜色可直接使用颜色名称(如green、red),也可以用十六进制色值(如#00B050、#FF0000)
  • 通过enumerate获取循环索引,确保每个系列匹配到对应的标记样式

内容的提问来源于stack exchange,提问作者Timothy A Calderwood

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 13:57:35