概述
以下是 maplotlib 庫(kù)中的繼承圖:

從上圖可以看出 Wedge 類繼承自 Patch 類,這就意味著 Wedge 類具有塊的相關(guān)屬性,也可以看出 Wedge 類是一個(gè)可以放到畫布中的實(shí)體類,官方文檔中對(duì)該類的定義如下:
A wedge centered at x, y center with radius r that sweeps theta1 to theta2 (in degrees). If width is given, then a partial wedge is drawn from inner radius r - width to outer radius r.
翻譯過來(lái)就是一個(gè)給定中心、半徑、起始角度和終止角度的扇形,wedge 類的定義如下:
class Wedge(Patch):
def __init__(self, center, r, theta1, theta2, width=None, **kwargs):
...
參數(shù)說(shuō)明:
- Center:指定扇形中點(diǎn);
- R:指定扇形半徑;
- Theta1:指定扇形起始角度;
- Theta2:指定扇形終止角度;
- Width:指定扇形寬度,如果給出寬度,則只會(huì)繪制部分扇形;
- Kwargs:其他 Patch 類關(guān)鍵字參數(shù);
參數(shù)詳解
角度
Theta1 和 theta2 角度共同控制扇形的角度,theta1 控制起始角度,theta2 控制終止角度。以 X 軸正向?yàn)?0°逆時(shí)針為正向,接下里本文將以多組角度幫助大家更好的理解相關(guān)參數(shù)。完整代碼如下:
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
wedge1 = Wedge((0, 0), 10, 0, -90, facecolor='red')
wedge2 = Wedge((0, 0), 10, 0, 150, facecolor='red')
wedge3 = Wedge((0, 0), 10, 0, 720, facecolor='red')
fig, ax = plt.subplots(1, 3)
ax[0].add_artist(wedge1)
ax[0].set_title("$theta1=0, theta=-90$")
ax[1].add_artist(wedge2)
ax[1].set_title("$theta1=0, theta=150$")
ax[2].add_artist(wedge3)
ax[2].set_title("$theta1=0, theta=720$")
for a in ax:
a.set_xlim([-10, 15])
a.set_ylim([-10, 15])
a.set_aspect('equal', adjustable='box')
plt.show()
畫圖結(jié)果如下:

Width
Width 參數(shù)控制扇形寬度,示例代碼如下:
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
wedge1 = Wedge((0, 0), 10, 0, 150, facecolor='red', width=3)
wedge2 = Wedge((0, 0), 10, 0, 150, facecolor='red', width=-3)
wedge3 = Wedge((0, 0), 10, 0, 150, facecolor='red', width=15)
fig, ax = plt.subplots(1, 3)
ax[0].add_artist(wedge1)
ax[0].set_title("$width=3$")
ax[1].add_artist(wedge2)
ax[1].set_title("$width=-3$")
ax[2].add_artist(wedge3)
ax[2].set_title("$width=15$")
for a in ax:
a.set_xlim([-10, 15])
a.set_ylim([-10, 15])
a.set_aspect('equal', adjustable='box')
plt.show()
畫圖結(jié)果如下:

應(yīng)用
Wedge 類的應(yīng)用主要集中在餅狀圖和環(huán)狀圖上,調(diào)用 pyplot.pie 函數(shù)的時(shí)候返回元祖的第一個(gè)元素就是 Wedge 對(duì)象列表,包含這個(gè)餅狀圖中所有的扇形。通過這個(gè)列表,我們可以調(diào)用 get_property() 和 set_property() 方法來(lái)獲取和設(shè)置扇形的屬性。完整示例代碼如下:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
recipe = ["375 g flour",
"75 g sugar",
"250 g butter",
"300 g berries"]
data = [float(x.split()[0]) for x in recipe]
ingredients = [x.split()[-1] for x in recipe]
def func(pct, allvals):
absolute = int(np.round(pct/100.*np.sum(allvals)))
return f"{pct:.1f}%\n({absolute:d} g)"
wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data),
textprops=dict(color="w"))
ax.legend(wedges, ingredients,
title="Ingredients",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))
plt.setp(autotexts, size=8, weight="bold")
ax.set_title("Matplotlib bakery: A pie")
plt.show()
畫圖結(jié)果如下:

文中難免會(huì)出現(xiàn)一些描述不當(dāng)之處(盡管我已反復(fù)檢查多次),歡迎在留言區(qū)指正,相關(guān)的知識(shí)點(diǎn)也可進(jìn)行分享,希望大家都能有所收獲!!如果覺得我的文章寫得還行,不妨支持一下。你的每一個(gè)轉(zhuǎn)發(fā)、關(guān)注、點(diǎn)贊、評(píng)論都是對(duì)我最大的支持!