openpyxl 设置x轴y轴
时间: 2023-10-18 17:24:16
浏览: 172
openpyxl 是一个 Python 库,用于[读写](https://geek.csdn.net/educolumn/1e4a90ddf8efac6c1c24c59859bbb66b?spm=1055.2569.3001.10083) Excel 文件。如果你想要设置 Excel 表格的 X 轴和 Y 轴,请参考以下示例[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083):
```python
from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference
# 创建一个工作簿
wb = Workbook()
ws = wb.active
# 写入数据
rows = [
['x', 'y'],
[1, 2],
[2, 4],
[3, 6],
[4, 8],
[5, 10]
for row in rows:
ws.append(row)
# 创建一个折线图
chart = LineChart()
# 设置 X 轴
x_data = Reference(ws, min_col=1, min_row=2, max_row=6)
chart.x_axis.title = 'X'
chart.x_axis.[scaling](https://geek.csdn.net/educolumn/3ae17deb2927a5c93aeef56ce2330364?spm=1055.2569.3001.10083).min = 1
chart.x_axis.[scaling](https://geek.csdn.net/educolumn/3ae17deb2927a5c93aeef56ce2330364?spm=1055.2569.3001.10083).max = 5
chart.add_data(x_data)
# 设置 Y 轴
y_data = Reference(ws, min_col=2, min_row=2, max_row=6)
chart.y_axis.title = 'Y'
chart.y_axis.[scaling](https://geek.csdn.net/educolumn/3ae17deb2927a5c93aeef56ce2330364?spm=1055.2569.3001.10083).min = 0
chart.y_axis.scaling.max = 10
chart.add_d
```