I'm creating charts in excel using openpyxl and I want some of my charts to have y-axes with the units in percent. Is this possible in openpyxl? It's quite easy in xlsxwriter, but I can't figure out how to do it in openpyxl. The code below is for a chart with two y-axes.
Thanks!
Edit:
Turns out it was as simple as changing the format of the y_axis, at least for this problem.
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")
I tried the following code and it worked for me
c1 .y_axis.number_format = '0%'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With