matplotlib饼图显示使用切片时的错误

yk9xbfzb  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(282)

使用matplotlib和饼图显示在其中使用silces时的错误

from matplotlib import pyplot as plt

slices = [7, 2, 2, 13, 15]

activities = ["Sleep", "Eat", "Play", "Study", "Entertainment"]

colors = ['c', 'm', 'r', 'b', 'g']

plt.pie(slices,
        labels=activities,
        colors=[colors],
        startangle=90,
        shadow=True,
        explode=(0, 0, 0, 0.1, 0),
        # autopct='What is written on every section',
        autopct='%1.1f%%'
        )

plt.legend()

plt.title("Pie Chart")

plt.show()
bwntbbo3

bwntbbo31#

你需要通过考试 colors 而不是 [colors] 作为论据。函数需要一个列表,而不是列表列表。
请在下面找到工作代码段:

import matplotlib.pyplot as plt

slices = [7,2,2,13,15]

activities = ["Sleep","Eat","Play","Study","Entertainment"]

colors = ['c','m','r','b','g']

plt.pie(slices,
        labels=activities,
        colors=colors,
        startangle=90,
        shadow=True,
        explode=(0,0,0,0.1,0),
        autopct='%1.1f%%'
        )

plt.legend()

plt.title("Pie Chart")

plt.show()

上述代码段生成以下输出:

相关问题