删除标签但在饼图中保留图例

Ghi*_*ADJ 7 python matplotlib

如何从饼图中删除标签但保留图例?

import matplotlib.pyplot as plt

x = [15, 30, 55]
labels = [1, 2, 3]

plt.figure(figsize=(3, 3), dpi=72)
plt.pie(x, labels=labels)
plt.legend()
plt.savefig('pie_1.png')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Imp*_*est 8

您可以labels从饼图中删除参数并将其添加到图例中。代替

plt.pie(x,labels=labels)
plt.legend()
Run Code Online (Sandbox Code Playgroud)

plt.pie(x)
plt.legend(labels=labels)
Run Code Online (Sandbox Code Playgroud)

完整示例:

import matplotlib.pyplot as plt

x = [15, 30, 55]
labels = [1, 2, 3]

plt.figure(figsize=(3, 3), dpi=72)
plt.pie(x)
plt.legend(labels=labels)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明