gpt4 book ai didi

python - Openpyxl 次要网格线

转载 作者:行者123 更新时间:2023-12-04 20:50:15 25 4
gpt4 key购买 nike

我正在开发一个 Python 应用程序,我在其中从设备收集数据,并尝试使用 Openpyxl 库将其绘制在 excel 文件中。我成功地完成了所有工作,包括绘制数据和格式化我制作的散点图,但是在向图中添加次要网格线时遇到了一些麻烦。
我觉得这绝对是可能的,因为在 API 中,我可以在 openpyxl.chart.axis module 下看到,有一个“minorGridlines”属性,但它不是 bool 输入(ON/OFF),而是一个 Chartlines 类。我试着去看看我会怎么做的兔子洞,但我想知道添加次要网格线的最直接的方法是什么?您是否必须手动构建图表线,或者有一种简单的方法吗?
我真的很感激任何帮助!

最佳答案

我想我已经回答了我自己的问题,但如果其他人需要这个,我会在这里发布(因为我在论坛上看不到这个问题的任何其他答案)。
示例代码(参见第 4、38 行):

# Imports for script
from openpyxl import Workbook # For plotting things in excel
from openpyxl.chart import ScatterChart, Reference, Series
from openpyxl.chart.axis import ChartLines
from math import log10

# Variables for script
fileName = 'testFile.xlsx'
dataPoints = 100

# Generating a workbook to test with
wb = Workbook()
ws = wb.active # Fill data into the first sheet
ws_name = ws.title

# We will just generate a logarithmic plot, and scale the axis logarithmically (will look linear)
x_data = []
y_data = []
for i in range(dataPoints):
x_data.append(i + 1)
y_data.append(log10(i + 1))

# Go back through the data, and place the data into the sheet
ws['A1'] = 'x_data'
ws['B1'] = 'y_data'

for i in range(dataPoints):
ws['A%d' % (i + 2)] = x_data[i]
ws['B%d' % (i + 2)] = y_data[i]

# Generate a reference to the cells that we can plot
x_axis = Reference(ws, range_string='%s!A2:A%d' % (ws_name, dataPoints + 1))
y_axis = Reference(ws, range_string='%s!B2:B%d' % (ws_name, dataPoints + 1))
function = Series(xvalues=x_axis, values=y_axis)

# Actually create the scatter plot, and append all of the plots to it
ScatterPlot = ScatterChart()
ScatterPlot.x_axis.minorGridlines = ChartLines()

ScatterPlot.x_axis.scaling.logBase = 10
ScatterPlot.series.append(function)
ScatterPlot.x_axis.title = 'X_Data'
ScatterPlot.y_axis.title = 'Y_Data'
ScatterPlot.title = 'Openpyxl Plotting Test'
ws.add_chart(ScatterPlot, 'D2')

# Save the file at the end to output it
wb.save(fileName)
解决方案背景:
我查看了 Openpyxl 的代码如何生成长轴网格线,它似乎遵循与短轴网格线类似的约定,我发现在“NumericAxis”类中,它们生成了带有以下行的主网格线(标记为 ' ##### 这一行 #####' 最初是从 'openpyxl->chart->axis' 文件中复制的):
class NumericAxis(_BaseAxis):

tagname = "valAx"

axId = _BaseAxis.axId
scaling = _BaseAxis.scaling
delete = _BaseAxis.delete
axPos = _BaseAxis.axPos
majorGridlines = _BaseAxis.majorGridlines
minorGridlines = _BaseAxis.minorGridlines
title = _BaseAxis.title
numFmt = _BaseAxis.numFmt
majorTickMark = _BaseAxis.majorTickMark
minorTickMark = _BaseAxis.minorTickMark
tickLblPos = _BaseAxis.tickLblPos
spPr = _BaseAxis.spPr
txPr = _BaseAxis.txPr
crossAx = _BaseAxis.crossAx
crosses = _BaseAxis.crosses
crossesAt = _BaseAxis.crossesAt

crossBetween = NestedNoneSet(values=(['between', 'midCat']))
majorUnit = NestedFloat(allow_none=True)
minorUnit = NestedFloat(allow_none=True)
dispUnits = Typed(expected_type=DisplayUnitsLabelList, allow_none=True)
extLst = Typed(expected_type=ExtensionList, allow_none=True)

__elements__ = _BaseAxis.__elements__ + ('crossBetween', 'majorUnit',
'minorUnit', 'dispUnits',)


def __init__(self,
crossBetween=None,
majorUnit=None,
minorUnit=None,
dispUnits=None,
extLst=None,
**kw
):
self.crossBetween = crossBetween
self.majorUnit = majorUnit
self.minorUnit = minorUnit
self.dispUnits = dispUnits
kw.setdefault('majorGridlines', ChartLines()) ######## THIS Line #######
kw.setdefault('axId', 100)
kw.setdefault('crossAx', 10)
super(NumericAxis, self).__init__(**kw)


@classmethod
def from_tree(cls, node):
"""
Special case value axes with no gridlines
"""
self = super(NumericAxis, cls).from_tree(node)
gridlines = node.find("{%s}majorGridlines" % CHART_NS)
if gridlines is None:
self.majorGridlines = None
return self
我试了一下,在像这样导入“Chartlines”类之后:
from openpyxl.chart.axis import ChartLines

我能够像这样向 x 轴添加次要网格线:
ScatterPlot.x_axis.minorGridlines = ChartLines()
至于格式化小网格线,我有点茫然,个人没有必要,但这至少是一个好的开始。

关于python - Openpyxl 次要网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65797060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com