from openpyxl import *
from openpyxl.chart import *
wb = load_workbook("videogamesales.xlsx")
ws = wb["Breakdown of Sales by Year"]
chart = LineChart()
chart.title = "Total Sales"
chart.x_axis.title = "Year"
chart.y_axis.title = "Breakdown of Sales by Year"
value = Reference(ws, min_col=2, max_col=5, min_row=1, max_row=40)
cats = Reference(ws, min_col=1, min_row=2, max_row=40)
chart.x_axis.delete = False
chart.y_axis.delete = False
chart.add_data(data=value, titles_from_data=True)
chart.set_categories(cats)
chart.style = 10
chart.width = 20
chart.height = 10
chart.legend.overlay = False
ws.add_chart(chart, "H2")
wb.save("videogamesales.xlsx")
The tutorial taught me I can make a clear graph without special attribute changing.
But it didn’t work.
I solved the problem the legend overlap the graph area using ‘chart.legend.overlay = False’
But I can’t find the method for x_axis, y_axis and title.
Please help me.
graph image
I want a clear graph
graph image I want to have
New contributor
STATBOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.