首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用openPyxl更改趋势线-颜色

使用openPyxl更改趋势线-颜色
EN

Stack Overflow用户
提问于 2020-01-28 23:02:35
回答 1查看 600关注 0票数 0

我正在尝试用Python和openpyxl编写一个脚本,它将数据保存在excel文件中,并绘制带有一些趋势线的散点图。openpyxl的问题是,默认模式下的趋势线始终是黑色的,所以我的问题是,如何将它们着色为不同的颜色。

代码语言:javascript
复制
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")```
EN

回答 1

Stack Overflow用户

发布于 2020-05-19 18:38:50

假设您希望您的线性趋势线是绿色的。然后..。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59951575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档