Matplotlib 教程 - 餅圖

Jinku Hu 2023年1月30日 2019年12月26日
  1. 順時針方向繪製的餅圖
  2. 餅圖與爆炸切片
Matplotlib 教程 - 餅圖

我們將在本教程中學習餅圖。

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt

x = np.array([15, 25, 30, 40])
label = ["France", "Germany", "Uk", "US"]

plt.pie(x, labels=label)
plt.show()

Matplotlib 餅圖

語法

matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None,
                      pctdistance=0.6, shadow=False, labeldistance=1.1,
                      startangle=None, radius=None, counterclock=True,
                      wedgeprops=None, textprops=None, center=(0, 0),
                      frame=False, hold=None, data=None)

引數

名稱 描述
label 標籤文字
fontdict 標籤文字字型字典,例如字型系列、顏色、粗細和大小
labelpad 標籤和 x 軸之間的間距,以 points 為單位

順時針方向繪製的餅圖

如果引數 counterclock 設定為 False,則餅形圖將按順時針方向繪製。

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt

x = np.array([15, 25, 30, 40])
label = ["France", "Germany", "Uk", "US"]

plt.pie(x, labels=label, counterclock=False)
plt.show()

Matplotlib 餅圖-順時針

餅圖與爆炸切片

explode 引數控制餅圖中的切片爆炸。它指定偏移每個楔形的半徑的分數。

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt

x = np.array([15, 25, 30, 40])
label = ["France", "Germany", "Uk", "US"]

plt.pie(x, labels=label, explode=(0.2, 0, 0, 0))
plt.show()

Matplotlib 餅圖-爆炸

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn