如何使用Python在Excel图表中添加彩色垂直带

时间:2018-10-18 13:25:05

标签: python excel openpyxl

我想使用openpyxl制作如下图:

enter image description here

要创建它,我遵循combining a line chart with a bar chart的方法,其中条形图被设置为背景垂直带。

我非常接近实现它,但是我无法做到:

  • 隐藏次要y轴的刻度线
  • 隐藏次要y轴的主要网格线
  • 使垂直条带的颜色透明,以便可以看到主要y轴的主要网格线

针对上述问题,我得到了以下结果:

sad plot

以下是产生以上图表的代码(使用openpyxl 2.3.5和Python 2.7.11):

from openpyxl import Workbook
from openpyxl.chart import BarChart
from openpyxl.chart import LineChart
from openpyxl.chart import Reference
from openpyxl.chart.data_source import NumFmt
from openpyxl.chart.marker import DataPoint
from openpyxl.chart.series_factory import SeriesFactory
from openpyxl.chart.axis import TextAxis
from openpyxl.chart.shapes import GraphicalProperties


workbook = Workbook()
worksheet = workbook.active

rows = [
    ['region', 'value', 'index', 'unity'],
    ['A1', 0.9, 1, 1],
    ['A2', 1.1, 2, 1],
    ['B1', 1.6, 3, 1],
    ['C1', 0.9, 4, 1],
    ['D1', 1.0, 5, 1],
    ['D2', 1.0, 6, 1],
    ['E1', 0.9, 7, 1],
]

for row in rows:
    worksheet.append(row)

verticalbands = BarChart()
verticalbands.type = "col"
verticalbands.grouping = "stacked"
verticalbands.overlap = 100
verticalbands.gapWidth = 0

colours = ["c4bedf", "c4bedf", "d6d8b4", "98d4e4", "e6b8b3", "e6b8b3", "aad2bf"]
xvalues = Reference(worksheet, min_col=3, min_row=2, max_row=8)
data = Reference(worksheet, min_col=4, min_row=2, max_row=8)
series = SeriesFactory(data)
verticalbands.x_axis = TextAxis()
verticalbands.set_categories(xvalues)
verticalbands.series.append(series)

for i, colour in zip(range(9), colours):
    pt = DataPoint(idx=i)
    pt.graphicalProperties = GraphicalProperties(solidFill=colour)
    series.dPt.append(pt)

verticalbands.y_axis.numFmt = NumFmt(formatCode=";")  #  hide value, see https://www.ablebits.com/office-addins-blog/2016/07/07/custom-excel-number-format/
verticalbands.y_axis.scaling.min = 0
verticalbands.y_axis.scaling.max = 1
verticalbands.y_axis.majorTickMark = None
verticalbands.y_axis.minorTickMark = None

data_chart = LineChart()
xvalues = Reference(worksheet, min_col=1, min_row=2, max_row=8)
yvalues = Reference(worksheet, min_col=2, min_row=1, max_row=8)
series = SeriesFactory(yvalues, title_from_data=True)
series.marker.symbol = 'circle'
series.marker.size = 4
series.marker.graphicalProperties.solidFill = "4F81BD"
series.marker.graphicalProperties.line.noFill = True
series.graphicalProperties.line.noFill = True
data_chart.series.append(series)
data_chart.x_axis = TextAxis()
data_chart.set_categories(xvalues)

verticalbands.y_axis.crosses = 'max'
verticalbands.y_axis.axId = 200

verticalbands += data_chart

worksheet.add_chart(verticalbands, "A12")

workbook.save('~/file.xlsx)

令人沮丧的是,这些行没有达到预期的效果:

verticalbands.y_axis.majorTickMark = None
verticalbands.y_axis.minorTickMark = None

要查看此问题,我必须将最右边的波段的透明度手动设置为50%:

enter image description here

简单地“删除”轴会很棒:

verticalbands.y_axis.delete = True

但这只是打破了图表:

enter image description here

为了透明起见,我尝试使用8-character RGB hex colours,但是solidFill的{​​{1}}参数不能采用GraphicalProperties这样的颜色,导致Excel无法打开无效文件。

关于如何克服这些问题的任何想法?

0 个答案:

没有答案