python 用 matplotlib 繪制圓環形嵌套餅圖步驟詳解


1、加載庫

import matplotlib as mpl import matplotlib.pyplot as plt

 

2、單層圓環餅圖

# 配置字體,顯示中文
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 配置坐標軸刻度值模式,顯示負號
mpl.rcParams['axes.unicode_minus'] = False # 定義數據
elements = ['面粉', '砂糖', '牛奶', '草莓醬', '堅果'] weight1 = [40, 15, 20, 10, 15] cs = ['red', 'orange', 'yellow', 'green', 'cyan'] # 對數據進行排序
x = list(zip(elements, weight1, cs)) x.sort(key=lambda e: e[1], reverse=True) [elements, weight1, cs] = list(zip(*x)) outer_cs = cs inner_cs = cs # 初始化圖表區
fig = plt.figure(figsize=(12, 8), facecolor='cornsilk' ) # 繪制外層圓環
wedges1, texts1, autotexts1 = plt.pie(x=weight1, autopct='%3.1f%%', radius=1, pctdistance=0.85, startangle=90, counterclock=False, colors=outer_cs, # 鍥形塊邊界屬性字典
                                      wedgeprops={'edgecolor': 'white', 'linewidth': 1, 'linestyle': '-' }, # 鍥形塊標簽文本和數據標注文本的字體屬性
                                      textprops=dict(color='k',  # 字體顏色
                                                     fontsize=14, family='Arial' ) ) # 繪制中心空白區域
plt.pie(x=[1], radius=0.6, colors=[fig.get_facecolor()] ) # 設置圖例
plt.legend(handles=wedges1, loc='best', labels=elements, title='配料表', facecolor = fig.get_facecolor(),    # 圖例框的填充顏色
           edgecolor='darkgray', fontsize=12 ) plt.title(s='果醬面包的配料表占比', color='blue', size=15, weight='bold' );

圖形:

 

3、內嵌圓環形餅圖

# 配置字體,顯示中文
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 配置坐標軸刻度值模式,顯示負號
mpl.rcParams['axes.unicode_minus'] = False # 定義數據
elements = ['面粉', '砂糖', '牛奶', '草莓醬', '堅果'] weight1 = [40, 15, 20, 10, 15] weight2 = [30, 25, 15, 20, 10] cs = ['red', 'orange', 'yellow', 'green', 'cyan'] # 對數據進行排序
x = list(zip(elements, weight1, weight2, cs)) x.sort(key=lambda e: e[1], reverse=True) [elements, weight1, weight2, cs] = list(zip(*x)) # 初始化圖表區
fig = plt.figure(figsize=(12, 8), facecolor='cornsilk' ) # 繪制外層圓環
wedges1, texts1, autotexts1 = plt.pie(x=weight1, autopct='%3.1f%%', radius=1, pctdistance=0.85, startangle=90, counterclock=False, colors=cs, # 鍥形塊邊界屬性字典
                                      wedgeprops={'edgecolor': 'white', 'linewidth': 1, 'linestyle': '-' }, # 鍥形塊標簽文本和數據標注文本的字體屬性
                                      textprops=dict(color='k',  # 字體顏色
                                                     fontsize=14, family='Arial' ) ) # 繪制內層圓環
wedges2, texts2, autotexts2 = plt.pie(x=weight2, autopct='%3.1f%%', radius=0.7, pctdistance=0.75, startangle=90, counterclock=False, colors=inner_cs, # 鍥形塊邊界屬性字典
                                      wedgeprops={'edgecolor': 'white', 'linewidth': 1, 'linestyle': '-' }, # 鍥形塊標簽文本和數據標注文本的字體屬性
                                      textprops=dict(color='k',  # 字體顏色
                                                     fontsize=14, family='Arial' ) ) # 繪制中心空白區域
plt.pie(x=[1], radius=0.4, colors=[fig.get_facecolor()] ) # 設置圖例
plt.legend(handles=wedges1, loc='best', labels=elements, title='配料表', facecolor = fig.get_facecolor(),    # 圖例框的填充顏色
           edgecolor='darkgray', fontsize=12 ) plt.title(s='不同果醬面包的配料表比較', color='blue', size=15, weight='bold' );

圖形:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM