openpyxl 绘制饼形图_openpyxl3.0官方文档(13)—— 饼图
餅圖?
餅圖將數(shù)據(jù)繪制為圓的切片,每個(gè)切片表示整個(gè)圓的百分比。切片按順時(shí)針方向繪制,0°位于圓的頂部。餅圖只接受一系列數(shù)值,標(biāo)題將默認(rèn)為系列的標(biāo)題。
1 from openpyxl import Workbook
2
3 from openpyxl.chart import (
4 PieChart,
5 ProjectedPieChart,
6 Reference
7 )
8 from openpyxl.chart.series import DataPoint
9
10 data = [
11 ['Pie', 'Sold'],
12 ['Apple', 50],
13 ['Cherry', 30],
14 ['Pumpkin', 10],
15 ['Chocolate', 40],
16 ]
17
18 wb = Workbook()
19 ws = wb.active
20
21 for row in data:
22 ws.append(row)
23
24 pie = PieChart()
25 labels = Reference(ws, min_col=1, min_row=2, max_row=5)
26 data = Reference(ws, min_col=2, min_row=1, max_row=5)
27 pie.add_data(data, titles_from_data=True)
28 pie.set_categories(labels)
29 pie.title = "Pies sold by category"
30
31 # Cut the first slice out of the pie
32 slice = DataPoint(idx=0, explosion=20)
33 pie.series[0].data_points = [slice]
34
35 ws.add_chart(pie, "D1")
36
37
38 ws = wb.create_sheet(title="Projection")
39
40 data = [
41 ['Page', 'Views'],
42 ['Search', 95],
43 ['Products', 4],
44 ['Offers', 0.5],
45 ['Sales', 0.5],
46 ]
47
48 for row in data:
49 ws.append(row)
50
51 projected_pie = ProjectedPieChart()
52 projected_pie.type = "pie"
53 projected_pie.splitType = "val" # split by value
54 labels = Reference(ws, min_col=1, min_row=2, max_row=5)
55 data = Reference(ws, min_col=2, min_row=1, max_row=5)
56 projected_pie.add_data(data, titles_from_data=True)
57 projected_pie.set_categories(labels)
58
59 ws.add_chart(projected_pie, "A10")
60
61 from copy import deepcopy
62 projected_bar = deepcopy(projected_pie)
63 projected_bar.type = "bar"
64 projected_bar.splitType = 'pos' # split by position
65
66 ws.add_chart(projected_bar, "A27")
67
68 wb.save("pie.xlsx")
投影餅圖?
投影餅圖從餅圖中提取一些切片并投影到第二個(gè)餅圖或條形圖中。當(dāng)數(shù)據(jù)系列中有很多較小的項(xiàng)時(shí),這非常有用。圖表可以按百分比、值(value)或位置進(jìn)行拆分。如果未設(shè)置任何內(nèi)容,則應(yīng)用程序?qū)Q定使用哪個(gè)。此外,還可以自定義拆分。
三維餅圖?
餅圖也可以用三維效果創(chuàng)建。
1 from openpyxl import Workbook
2
3 from openpyxl.chart import (
4 PieChart3D,
5 Reference
6 )
7
8 data = [
9 ['Pie', 'Sold'],
10 ['Apple', 50],
11 ['Cherry', 30],
12 ['Pumpkin', 10],
13 ['Chocolate', 40],
14 ]
15
16 wb = Workbook()
17 ws = wb.active
18
19 for row in data:
20 ws.append(row)
21
22 pie = PieChart3D()
23 labels = Reference(ws, min_col=1, min_row=2, max_row=5)
24 data = Reference(ws, min_col=2, min_row=1, max_row=5)
25 pie.add_data(data, titles_from_data=True)
26 pie.set_categories(labels)
27 pie.title = "Pies sold by category"
28
29
30 ws.add_chart(pie, "D1")
31
32 wb.save("pie3D.xlsx")
漸變餅圖?
也可以使用漸變序列創(chuàng)建餅圖。
總結(jié)
以上是生活随笔為你收集整理的openpyxl 绘制饼形图_openpyxl3.0官方文档(13)—— 饼图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 图片地址_python解码
- 下一篇: cbow word2vec 损失_wor