Search code examples
pythonmatplotlibpie-chartdonut-chart

three level pie plot matplotlib - how to "prettify"


I am trying to mock up a three-level pie plot in matplotlib but unhappy with the aesthetics of it.

enter image description here

Specifically, I cant seem to make the three levels (or donuts) more distinctive. Also, I would like to show the labels as a legend ideally and not how they appear currently. Finally, I would appreciate a complementary set of 8 colors if possible.

My code follows below:

import matplotlib.pyplot as plt
import numpy as np

first_labels = ["B", "S", "D", "SG", "OBGL", "G", 'T', "O", "I"]
first_sizes = [2000, 300, 200, 100, 100, 150, 40, 30, 700]

second_sizes = [1000, 200, 200, 400, 500, 40, 1, 1, 1000]

third_sizes = [500, 300, 400, 500, 400, 100, 5, 2, 800]

flatui = (sns.diverging_palette(20, 250, n=8))


bigger = plt.pie(first_sizes, labels=first_labels, colors=flatui,
                 startangle=90, frame=True, radius = 1)


smaller = plt.pie(second_sizes,
                  colors=flatui, radius=0.9,
                  startangle=90, labeldistance=0.9)



smallest = plt.pie(third_sizes,
                  colors=flatui, radius=0.8,
                  startangle=90, labeldistance=0.8)


centre_circle = plt.Circle((0, 0), 0.7, color='white', linewidth=0)


fig = plt.gcf()
fig.gca().add_artist(centre_circle)

plt.axis('equal')
plt.tight_layout()

plt.show()

Can someone please suggest how to "prettify" the pie chart?


Solution

  • from the documentation of plt.pie and this page:

    [...]
    wedgeprops : dict, optional, default: None
    Dict of arguments passed to the wedge objects making the pie. For example, you can pass in wedgeprops = {'linewidth': 3} to set the
    width of the wedge border lines equal to 3. For more details, look at
    the doc/arguments of the wedge object. By default clip_on=False.
    [...]
    

    you can customise the wedges by feeding a dictionary called wedgeprops to your pie. For instance:

    [...] repeating your code [...]
    
    bigger = plt.pie(first_sizes, labels=first_labels, colors=flatui,
                     startangle=90, frame=True, radius = 1,
                    wedgeprops={'edgecolor':'k'})
    
    
    smaller = plt.pie(second_sizes,
                      colors=flatui, radius=0.9,
                      startangle=90, labeldistance=0.9,
                     wedgeprops={'edgecolor':'k'})
    
    
    
    smallest = plt.pie(third_sizes,
                      colors=flatui, radius=0.8,
                      startangle=90, labeldistance=0.8,
                      wedgeprops={'edgecolor':'k'})
    
    centre_circle = plt.Circle((0, 0), 0.7, color='white', linewidth=0)
    
    fig = plt.gcf()
    fig.gca().add_artist(centre_circle)
    
    # add legend to current ax:
    plt.gca().legend(loc='center right', bbox_to_anchor=(1,0,0.5,0.5))
    

    enter image description here

    If you want the labels only in the legend and not on the plot:

    # make first pie without labels:
    bigger = plt.pie(first_sizes, colors=flatui,
                     startangle=90, frame=True, radius = 1,
                    wedgeprops={'edgecolor':'k'})
    
    # feed labels to legend:
    plt.gca().legend(first_labels, loc='center right', bbox_to_anchor=(1,0,0.5,0.5))
    

    enter image description here