Python Forum
Spacing pie chart colours evenly in matplotlib?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spacing pie chart colours evenly in matplotlib?
#1
How can I make each slice of the pie chart have a different colour?

[Image: iCheY7e.png]

this is the code:

import pandas as pd
import matplotlib
from matplotlib import pyplot as plt

#the data is taken from a sql database
data = pd.read_sql(sql,connection)

not_null_industry = data[pd.notnull(data['Industry'])]
not_null_industry['count'] = 1
count = not_null_industry.groupby(['Industry']).count()['count']

industries = not_null_industry['Industry'].unique()
industries.sort()
deals = count.values

#map industry to number of deals to keep colors consistent later
industry_deals = {}
for (industry,deal) in zip(industries,deals):
    industry_deals[industry] = deal

#turn dictionary into list of tuples to pass into pie chart later
sorted_deals = sorted(industry_deals.items(),key=lambda x:x[1],reverse=True)

#split industries from number of deals to pass into the pie chart
sorted_industries = [x[0] for x in sorted_deals]
sorted_values = [x[1] for x in sorted_deals]

colormap = plt.cm.get_cmap('Set1')
colornorm = matplotlib.colors.Normalize(vmin=0.0,vmax=len(industries))

colors = []

#assign a color to each industry and the corresponding patch in the legend
handles = []
for i,l in enumerate(industries):
    handles.append(matplotlib.patches.Patch(color=plt.cm.Set1((colornorm((i))))))
    colors.append(colormap((colornorm(i))))
    print(colornorm(i))
    
#pass split value into pie chart
patches, texts = plt.pie(sorted_values,colors=colors,startangle=90)
plt.legend(handles,sorted_industries, bbox_to_anchor=(0.85,1.025),loc='upper left')
plt.axis('equal')
plt.tight_layout()
plt.show()
this is the output of printing the color normalisations:
[Image: Lf2GPug.png]

I think the colours are grouped together because the intervals produced by colornorm are too close together. I've tried multiplying i by numbers like 2 and 4, which works, until i exceeds the numbers of colours in the set, and then all the colours are mapped to vmax (which i've defined as the length of the list of industries in the data). increasing the vmax value by a corresponding amount just re-presents the issue.

Is there a better way of doing this?
Reply
#2
I think the main problem here is that you were used the Set1 colormap. This colormap can handle only 9 colors. Consider using continuous colormap, e.g. bwr or something else.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question I’m trying to create a Power BI Matplotlib quadrant chart and I’m a little stumped. Nidwolff 1 325 Mar-04-2024, 06:07 AM
Last Post: Danishhafeez
  Rename labels of a bar chart Matplotlib smalatray 1 4,286 Jul-01-2020, 01:48 AM
Last Post: hussainmujtaba
  Matplotlib bar chart ollarch 0 1,372 Mar-04-2020, 10:45 AM
Last Post: ollarch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020