如何在饼图matplotlib上生成更多颜色

时间:2014-01-10 01:52:35

标签: python matplotlib pie-chart

我的图表中显示的内容超过40件。 我只有10种颜色反复显示在图表上。如何生成更多颜色。

plt.pie(f,labels=labels,autopct='%1.1f%%', startangle=90,shadow=True)

我应该添加“color = colors”,其中颜色是无限生成的?

3 个答案:

答案 0 :(得分:24)

您需要colors参数,旁边您可以使用cm中的一些颜色贴图。

>>> import matplotlib.pyplot as plt
>>> from matplotlib import cm
>>> import numpy as np
>>> a=np.random.random(40)
>>> cs=cm.Set1(np.arange(40)/40.)
>>> f=plt.figure()
>>> ax=f.add_subplot(111, aspect='equal')
>>> p=plt.pie(a, colors=cs)
>>> plt.show()

enter image description here

除了使用色彩映射外,还要考虑使用.set_color_cycle()方法。请参阅此帖子:plotting different colors in matplotlib

答案 1 :(得分:1)

我希望这个答案会有用。检查此链接, Matplotlib supported colors。 您可以从中随机选择40种颜色并在饼图中使用。

mcolors.TABLEAU_COLORS
mcolors.BASE_COLORS
mcolors.CSS4_COLORS

样品

import random
import matplotlib.colors as mcolors
colors = random.choices(list(mcolors.CSS4_COLORS.values()),k = number_of_colors)

答案 2 :(得分:-1)

如果使用上述解决方案时您的饼图是由相同颜色的分组块组成的,请尝试通过以下链接将颜色映射表从“ Set1”更改为所需的任何映射表:this question

关于随机化颜色,我建议您在上述解决方案中随机化CS数组。但这实际上并不能提供很好的颜色范围。