Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate more colors on pie chart matplotlib

I am having more then 40 items to show in my chart. I have only 10 colours that repeatedly are shown on the chart. How can I generate more colors.

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

I should add "color=colors" where colors is generated infinitely ?

like image 444
user3001937 Avatar asked Jan 10 '14 01:01

user3001937


2 Answers

You need colors argument, beside that you can use some color maps from 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

Beside using colormaps, also consider using .set_color_cycle() method. See this post: plotting different colors in matplotlib

like image 183
CT Zhu Avatar answered Oct 19 '22 10:10

CT Zhu


I hope this answer would be useful. Check this link , Matplotlib supported colors. You can randomly pick 40 colors from it and use in your pie chart.

mcolors.TABLEAU_COLORS
mcolors.BASE_COLORS
mcolors.CSS4_COLORS

Sample

import random
import matplotlib.colors as mcolors
colors = random.choices(list(mcolors.CSS4_COLORS.values()),k = number_of_colors)
like image 39
Gags08 Avatar answered Oct 19 '22 10:10

Gags08