面积图

Area Charts


2D面积图

Area charts are similar to line charts with the addition that the area underneath the plotted line is filled. Different variants are available by setting the grouping to “standard”, “stacked” or “percentStacked”; “standard” is the default.

面积图与线性图类似;描绘的线下面进行填充,用面来表示。通过设置grouping不同的值(chart.grouping="standard")绘制不同的面积图,包括”standard”,”stacked”,”percentStacked”,默认为 standard。

  1. from openpyxl import Workbook
  2. from openpyxl.chart import (
  3. AreaChart,
  4. Reference,
  5. Series,
  6. )
  7. wb = Workbook()
  8. ws = wb.active
  9. rows = [
  10. ['Number', 'Batch 1', 'Batch 2'],
  11. [2, 40, 30],
  12. [3, 40, 25],
  13. [4, 50, 30],
  14. [5, 30, 10],
  15. [6, 25, 5],
  16. [7, 50, 10],
  17. ]
  18. for row in rows: # 为worksheet写入多行数据
  19. ws.append(row) # 将list作为一行数据写入worksheet中
  20. chart = AreaChart() # 创建 2D图表 对象
  21. chart.title = "Area Chart" # 设置图表标题
  22. chart.style = 13 # 设置图标风格
  23. # chart.grouping="standard" # 设置图表 grouping 类型
  24. chart.x_axis.title = 'Test' # 设置x轴名称
  25. chart.y_axis.title = 'Percentage' # 设置y轴名称
  26. cats = Reference(ws, min_col=1, min_row=1, max_row=7) # 关联分类数据的值
  27. data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7) # 关联构图数据的值
  28. chart.add_data(data, titles_from_data=True) # 为图表添加数据
  29. chart.set_categories(cats) # 为图表添加分类
  30. ws.add_chart(chart, "A10") # 设置图表位置
  31. wb.save("area.xlsx")

"Sample area charts"

3D面积图

创建3D面积图

  1. from openpyxl import Workbook
  2. from openpyxl.chart import (
  3. AreaChart3D,
  4. Reference,
  5. Series,
  6. )
  7. wb = Workbook()
  8. ws = wb.active
  9. rows = [
  10. ['Number', 'Batch 1', 'Batch 2'],
  11. [2, 30, 40],
  12. [3, 25, 40],
  13. [4 ,30, 50],
  14. [5 ,10, 30],
  15. [6, 5, 25],
  16. [7 ,10, 50],
  17. ]
  18. for row in rows:
  19. ws.append(row) # 绘制单元格数据
  20. chart = AreaChart3D() # 创建 3D面积图 对象
  21. chart.title = "Area Chart"
  22. chart.style = 13
  23. chart.x_axis.title = 'Test'
  24. chart.y_axis.title = 'Percentage'
  25. chart.legend = None
  26. cats = Reference(ws, min_col=1, min_row=1, max_row=7) # 关联分类数据的值
  27. data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7) #关联构图数据的值
  28. chart.add_data(data, titles_from_data=True)
  29. chart.set_categories(cats)
  30. ws.add_chart(chart, "A10")
  31. wb.save("area3D.xlsx")

这样就创建了一个简单的 3D面积图。 面积图的 Z轴 可以用来作为数据说明。

"Sample 3D area chart with a series axis"


^ <-Previous | Next-> |