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

问题描述 投票:9回答:2

我在我的图表中显示了超过40个项目。我只有10种颜色反复显示在图表上。如何生成更多颜色。

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

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

python matplotlib pie-chart
2个回答
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()

除了使用colormaps,还可以考虑使用.set_color_cycle()方法。看到这篇文章:plotting different colors in matplotlib


-1
投票

如果您的饼图在使用上述解决方案时使用相同颜色的分组块,请尝试从以下链接将颜色贴图从“Set1”更改为您喜欢的任何地图:https://matplotlib.org/examples/color/colormaps_reference.html

至于随机化颜色,我建议你在上面的解决方案中随机化cs数组。但这并没有真正给出一个很好的色彩范围。

© www.soinside.com 2019 - 2024. All rights reserved.