Python更改图表图例颜色-Openpyxl [英] Python Changing Chart Legend Colors - Openpyxl

查看:1702
本文介绍了Python更改图表图例颜色-Openpyxl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to change the color associated with the legend of a chart created in Openpyxl?

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

I have created a relatively simple chart using my data but would like to force the colors it uses to create each piece. Here's the snippet of my code related to the chart:

chart = PieChart()
data = Reference(worksheet, min_col=2, min_row=4, max_row=7, max_col=2)
categories = Reference(worksheet, min_col=1, min_row=4, max_row=7, max_col=1)

chart.add_data(data)
chart.set_categories(categories)

worksheet.add_chart(chart, 'D3')

推荐答案

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

If you want to change the colors of each series in the legend, you should change the color of the lines for each series in your plot using something like this:

series.graphicalProperties.line.solidFill =颜色

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)

这篇关于Python更改图表图例颜色-Openpyxl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆