使用openPyxl更改趋势线颜色

问题描述 投票:0回答:1

我正在尝试使用openpyxl用Python编写脚本,该脚本将数据保存在excel文件中,并且绘制了带有某些趋势线的散点图。 openpyxl的问题在于,默认模式下的趋势线始终被涂成黑色,所以我的问题是如何用不同的颜色为其着色。

from openpyxl.chart import * 
from openpyxl.chart.trendline import Trendline 
from openpyxl.styles import *
from openpyxl.chart.shapes import *
from openpyxl.drawing.colors import *

wb = Workbook()
ws = wb.create_sheet(name)

    #writes the Headlines of the columns into excel-file
for c in range(1,5):
    ws.cell(1, c).value = ueberschriften[c-1]

    #writes the data into excel-file
for c in range(2, points+2): 
    e = c-2
    for d in range(1,5):
        f = d-1
        b = a[e][f]
        ws.cell(c, d).value = b

for row in a:
    ws.append(row)        

chart = ScatterChart()
chart.title = "Measuring"
chart.style = 13
chart.x_axis.title = 'Time'
chart.y_axis.title = 'Value'

xvalues = Reference(ws, min_col=1, min_row=2, max_row=points+1)
for i in range(2, 5):
    values = Reference(ws, min_col=i, min_row=1, max_row=points+1)
    series = Series(values, xvalues, title_from_data=True)
    chart.series.append(series)

l = chart.series[0]
l.graphicalProperties.line.solidFill = "FF0000"
l.trindline = Trendline(trendlineType = 'poly', order = fit_order) #trendline with polinomial fitting

l1 = chart.series[1]
l1.graphicalProperties.line.solidFill = "0000FF"
line1.trendline = Trendline(trendlineType = 'poly', order = fit_order)

l2 = chart.series[2]
l2.graphicalProperties.line.solidFill = "00FF00"
l2.trendline = Trendline(trendlineType = 'poly', order = fit_order)

ws.add_chart(chart, "A10")

wb.save("C:\****\****\testFile.xlsx")```

python excel charts openpyxl trendline
1个回答
0
投票

假设您希望线性趋势线变为绿色。然后...

from openpyxl.chart.trendline import Trendline
from openpyxl.chart.shapes import GraphicalProperties
from openpyxl.drawing.line import LineProperties

line_props = LineProperties(solidFill='00FF00')
g_props = GraphicalProperties(ln=line_props)
linear_trendline = Trendline(spPr=g_props)
my_chart.series[0].trendline = linear_trendline
© www.soinside.com 2019 - 2024. All rights reserved.