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

如何使用openpyxl旋转并移动图表坐标轴标题

使用openpyxl设置Y轴标题水平旋转并移至顶部

要实现Y轴标题水平显示并移到轴顶部,只需在设置标题文本后,添加两行代码分别控制旋转角度和位置:

# 设置Y轴标题文本(保留你原有的代码)
chart.y_axis.title = "g²/Гц" if sheet.cell(row=2, column=columnOffset + col_index).value == "Sxx" else "g"
# 将标题设置为水平旋转(角度0)
chart.y_axis.title.rotation = 0
# 把标题移到Y轴顶部
chart.y_axis.title.position = "t"

如果偏好使用枚举类型(更具可读性),可以先导入TitlePositionType,再设置位置:

from openpyxl.chart.text import TitlePositionType

# 前面的标题设置代码不变
chart.y_axis.title.position = TitlePositionType.TOP

说明:

  • rotation = 0:将标题旋转为水平方向(默认垂直显示时rotation为90)
  • position = "t" 或 TitlePositionType.TOP:指定标题显示在Y轴的顶部位置

内容的提问来源于stack exchange,提问作者Pr1nceSS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 18:18:11