openpyxl设置条形图中的条形颜色

时间:2018-07-02 16:41:22

标签: bar-chart openpyxl

我想使用openpyxl在条形图中设置条形的颜色。我创建了以下

data = Reference(sheet, min_col=3, min_row=6, max_col=4, max_row=10)
titles = Reference(sheet, min_col=1, min_row=6, max_row=10)
chart = BarChart3D()
chart.title = title
chart.add_data(data=data, titles_from_data=True)
chart.set_categories(titles)

我在这里找到How to set Background Color of Plot Area of Chart using openpyxl 如何使用chart.plot_area.graphical_properties更改背景颜色

但是,我不知道更改条形的颜色。

1 个答案:

答案 0 :(得分:2)

对于2D图,我使用graphicalProperties.line.solidFillgraphicalProperties.solidFill

wb = load_workbook('data.xlsx')
ws = wb['sheet1']

chart = BarChart()
chart.type = "col"
chart.style = 10
chart.title = "Chart Title"
chart.y_axis.title = 'Y Axis'
chart.x_axis.title = 'X Axis'

data = Reference(ws, min_col=3, min_row=1, max_row=3, max_col=3)
cats = Reference(ws, min_col=1, min_row=2, max_row=3)

chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)
chart.shape = 4

# Change bar filling and line color 
s = chart.series[0]
s.graphicalProperties.line.solidFill = "00000"
s.graphicalProperties.solidFill = "ff9900" 


ws.add_chart(chart, "A10")
wb.save("bar.xlsx")

我希望3D图也一样