我正在使用openpyxl创建Excel图表,我希望其中一些图表的y轴以百分比为单位。在openpyxl中是否有这种可能性?在xlsxwriter中非常容易实现,但我无法弄清如何在openpyxl中实现。下面的代码是用于具有两个y轴的图表。
谢谢!
编辑:
事实证明,对于这个问题,只需更改y轴的格式即可轻松解决。
谢谢!
编辑:
事实证明,对于这个问题,只需更改y轴的格式即可轻松解决。
c2.y_axis.number_format = '0%'
def line_chart_2axis(sheet_name, file_name):
font = Font(color = "000000", size = 8.5, name = 'Avenir Next Condensed Regular')
c1 = LineChart()
data1 = Reference(data_sheet, min_col=3, min_row=1, max_col=3, max_row=len(df1)+1)
dates = Reference(data_sheet, min_col=1, min_row=2, max_col=1, max_row=len(df1)+1)
c1.add_data(data1, titles_from_data=True)
c1.title = "Title"
c1.style = 5
c1.y_axis.title = 'Y Axis 1'
c1.x_axis.number_format = 'Mmm-YYYY'
c1.set_categories(dates)
c1.y_axis.majorGridlines = None
c1.legend.position = 'b'
s1 = c1.series[0]
s1.graphicalProperties.line.width = 14000
s1.graphicalProperties.line.solidFill = "8989ff"
c2 = LineChart()
data2 = Reference(data_sheet, min_col=2, min_row=1, max_col=2, max_row=len(df1)+1)
c2.add_data(data2, titles_from_data=True)
c2.y_axis.axId = 200
c2.y_axis.title = "Y Axis 2"
c2.y_axis.crosses = "max"
c2.y_axis.majorGridlines = None
s2 = c2.series[0]
s2.graphicalProperties.line.width = 14000
s2.graphicalProperties.line.solidFill = "000062"
c1 += c2
sheet.add_chart(c1, "B2")