14

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 ?

Paweł Fus
  • 44,795
  • 3
  • 61
  • 77
user3001937
  • 2,005
  • 4
  • 19
  • 23

3 Answers3

38

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

CT Zhu
  • 52,648
  • 17
  • 120
  • 133
  • 5
    When I run your code verbatim, I get a lot of repeated colors. [Here is an image](https://i.stack.imgur.com/KQGdU.png) of the outputted pie chart. Do you know how I can create a pie chart that has no repeated colors, while also at the same time not grouping those colors together? (As I do not want the pie chart to look like a rainbow; I want the order of colors to be random.) Thank you. – Crickets Oct 20 '18 at 04:35
5

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)
Gags08
  • 240
  • 2
  • 9
2

If your pie chart is coming out with grouped chunks of the same colour when utilising the solution above, try changing the colour map from 'Set1' to any map you like from the following link: https://matplotlib.org/examples/color/colormaps_reference.html

As for randomising the colours, I suggest that you randomise the cs array in the solution above. But that doesn't really give a nice spectrum of colours.

DecaX
  • 31
  • 1