from openpyxl import Workbook
from openpyxl.chart import BarChart,LineChart, Reference
from copy import deepcopy
wb = Workbook(write_only=True)
ws = wb.create_sheet()
rows = [
('Number', 'Batch 1', 'Batch 2', 'Batch 3'),
(2, 10, 30,40),
(3, 40, 60,50),
(4, 50, 70,60),
(5, 20, 10,70),
(6, 10, 40,80),
(7, 50, 30,90),
]
for row in rows:
ws.append(row)
chart1 = BarChart()
chart1.type = "col"#纵向柱形图
chart1.style = 14
chart1.title = "Bar Chart"#图表标题
chart1.y_axis.title = 'Test number'#纵坐标轴标题
chart1.x_axis.title = 'Sample length (mm)'#横坐标轴标题
data = Reference(ws, min_col=2, max_col=3, min_row=1, max_row=7)#数据引用范围,
cats = Reference(ws, min_col=1, max_col=1, min_row=2, max_row=7)#类别对象
chart1.add_data(data, titles_from_data=True)
chart1.set_categories(cats)#传入类别范围
chart1.shape = 4
ws.add_chart(chart1, "A10")
chart2 = LineChart()
chart2.type = "col"#纵向柱形图,bar横向
chart2.style = 10
chart2.title = "Line Chart"#图表标题
chart2.y_axis.title = 'Test number'#纵坐
利用openpyxl在excel中作图
最新推荐文章于 2022-02-23 09:09:00 发布