饼图还可以将多个饼图进行嵌套,展示多数据集的比例分布情况。将多组数据进行比较。
# -*- coding: UTF-8 -*-
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
data1 = [4, 9, 5, 10, 7]
data2 = [7, 4, 5, 8, 7]
labels = ["哈士奇", "贵宾犬", "吉娃娃", "柴犬", "藏獒"]
sum1 = sum(data1)
sum2 = sum(data2)
num1 = []
num2 = []
for i in data1:
num1.append(i/sum1)
for i in data2:
num2.append(i/sum2)
wedges1, texts1, autotexts1 = plt.pie(num1,
autopct="%3.1f%%",
radius=1,
pctdistance=0.8,
colors=["r", "g", "b", "y", "orange"],
textprops=dict(color="w"),
wedgeprops=dict(width=0.4, edgecolor="w"))
wedges2, texts2, autotexts2 = plt.pie(num2,
autopct="%3.1f%%",
radius=0.6,
pctdistance=0.7,
colors=["r", "g", "b", "y", "orange"],
textprops=dict(color="w"),
wedgeprops=dict(width=0.4, edgecolor="w"))
plt.legend(wedges1,
labels,
fontsize=12,
title="狗的种类",
loc="center right",
bbox_to_anchor=(0.9, 0, 0.3, 1))
plt.setp(autotexts1, size=15)
plt.setp(autotexts2, size=15)
plt.title("不同类型狗的数量所占的百分比",size=17)
plt.show()