openpyxl - chart doesn't show axes

Viewed 1065

Hi I'm new to openpyxl and I'm reading the documentation. Here's the code that I write based on the docs.

from openpyxl import Workbook
from openpyxl.chart import (BarChart, Reference)

book = Workbook()

sheet = book.active

rows = (
    ('USA', 46),
    ('China', 38),
    ('UK', 29),
    ('Russia', 22),
    ('South Korea', 13),
    ('Germany', 11)
)

for row in rows:
    sheet.append(row)

chart = BarChart()

data = Reference(sheet, min_row = 1, max_row=6, min_col=2, max_col=2)
cats = Reference(sheet, min_col=1, min_row=1, max_row=6)

chart.add_data(data)
chart.set_categories(cats)

sheet.add_chart(chart, 'A8')

book.save('chart.xlsx')

I ran the code then opened the file using LibreOffice the chart doesn't show axes. I have to manually 'Insert/Delete Axis' for them to show up. Is this due to the code or LibreOffice? Can anyone point me out?

1 Answers

I was having the same problem. After generating graphics with openpyxl, the axes of these were not displayed in Libreoffice but in Excel. After a lot of tests I've managed to solve it, I've done the following:

chart.x_axis.delete = False
chart.y_axis.delete = False

I hope that my first response in stackoverflow can help you.

Related