面积图和折线图很相似,区别是面积图会填充绘制线的下方区域,我们还是用折线图里的数据进行操作
from openpyxl import Workbook
from openpyxl.chart import (
AreaChart,
Reference,
Series,
)
wb = Workbook()
ws = wb.active
rows = [
['月份', '1店', '2店', '3店'],
['2021-01', 20, 30, 10],
['2021-02', 23, 25, 15],
['2021-03', 34, 30, 20],
['2021-04', 30, 40, 40],
['2021-05', 40, 35, 30],
['2021-06', 50, 50, 35],
['2021-07', 50, 55, 45],
['2021-08', 55, 60, 50],
['2021-09', 45, 70, 55],
['2021-10', 60, 75, 50],
['2021-11', 55, 80, 55],
['2021-12', 50, 80, 70],
]
for row in rows:
ws.append(row)
chart = AreaChart()
chart.title = "Area Chart"
chart.style = 13
chart.x_axis.title = '面积图'
chart.y_axis.title = '销售额'
cats = Reference(ws, min_col=1, min_row=2, max_row=13)
data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=13)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)
ws.add_chart(chart, "A15")
wb.save("./data/面积图.xlsx")
效果图
只需要修改两处代码就能生成3D面积图
# 引入AreaChart3D
from openpyxl.chart import (
AreaChart3D,
Reference,
Series,
)
chart = AreaChart3D() # 创建3D面积图
效果图
QQ交流群: 211426309