Python更改图表图例颜色 - Openpyxl

时间:2015-09-28 20:18:18

标签: python openpyxl

是否可以更改与Openpyxl中创建的图表的图例相关联的颜色?

我使用我的数据创建了一个相对简单的图表,但是想强制它用来创建每个部分的颜色。这是我的代码与图表相关的片段:

set(groot,'defaultUicontrolFontSize', 18)

2 个答案:

答案 0 :(得分:0)

更改颜色的最简单方法是通过样式更改颜色方案:

chart.style = 5

还有其他两种方法我没有考虑过,但您可以使用更加模糊的方法修改颜色。

第一个是通过这个班级的成员:

openpyxl.ledgend.Legend

期望分配GraphicalProperties的spPr成员,您可以修改它以更改颜色。

第二种可能性是创建自己的样式并使用前面提到的命令进行分配。

答案 1 :(得分:0)

如果要更改图例中每个系列的颜色,则应使用以下内容更改绘图中每个系列的线条颜色:

  

series.graphicalProperties.line.solidFill = Color

from openpyxl.chart import ScatterChart, Reference, Series

# Set up basic XY Scatter Chart
chart = ScatterChart()
chart.title = "Scatter Chart"
chart.style = 2 # Excel menu chart tools: Design > Chart Styles > picked second style

# setup the X-axis series (A2:A7). Notice that header is excluded in range by starting on row 2.
xvalues = Reference(ws, min_col=1, min_row=2, max_row=7)

# setup, style and append the first series (B1:B7)
values = Reference(ws, min_col=2, min_row=1, max_row=7)
series = Series(values, xvalues, title_from_data=True)
series.smooth = True
series.graphicalProperties.line.width = 50000
series.graphicalProperties.line.solidFill = 'FF0000' # Red
chart.append(series)
相关问题