openpyxl 设置条形图中条形的颜色
openpyxl set color of bar in bar chart
我想用 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)
我在这里找到
如何使用 chart.plot_area.graphical_properties
更改背景颜色
但是,我不知道要更改条形的颜色。
对于 2D 图,我使用 graphicalProperties.line.solidFill
和 graphicalProperties.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图也一样
我将 one_color
图表更改为具有 chart.varyColors
属性的 multiple_color
图表:
chart.varyColors = "0000FFFF"
字符串"0000FFFF"
是openpyxl模块中的颜色值,可以自己选择
https://openpyxl.readthedocs.io/en/stable/styles.html#colours
我想用 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)
我在这里找到
但是,我不知道要更改条形的颜色。
对于 2D 图,我使用 graphicalProperties.line.solidFill
和 graphicalProperties.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图也一样
我将 one_color
图表更改为具有 chart.varyColors
属性的 multiple_color
图表:
chart.varyColors = "0000FFFF"
字符串"0000FFFF"
是openpyxl模块中的颜色值,可以自己选择
https://openpyxl.readthedocs.io/en/stable/styles.html#colours