Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Nested pie chart graphing function - put legend in subplot

def pie_chart_nested(labels, sizes, colors, labels_sub, sizes_sub, colors_sub, skip, end, right):
    
    plt.figure(figsize = (10,8))

    #outer donut
    wedgeprops = {'linewidth': 4, "edgecolor": "w"}
    textprops = {"fontsize":16}

    plt.pie(sizes, colors = colors, labels = labels, autopct = '%1.1f%%', 
        startangle=90, pctdistance=0.85, textprops = textprops, wedgeprops = wedgeprops)
    
    #inner donut
    plt.pie(sizes_sub, colors = colors_sub, labels = labels_sub, autopct = '%1.1f', radius=0.7, startangle=90, 
            labeldistance = None, pctdistance =0.78, wedgeprops = wedgeprops)
    
    #legend formatting
    handles, labels = plt.gca().get_legend_handles_labels()   #get handles & labels of the chart

                       #skip legend of the outer donut, #only display legend of the inner donut
    plt.legend(handles[skip:skip + end], labels[skip:skip + end], bbox_to_anchor = (1,0.5), loc = 'upper right',
               fontsize = 15, bbox_transform=plt.gcf().transFigure) 
    
    #put legend on subplot, avoid overlapping with pie chart
    plt.subplots_adjust(left=0, bottom=0.1, right=right) 
    
    centre_circle = plt.Circle((0,0),0.4,fc='white')
    plt.gcf().gca().add_artist(centre_circle)      
    
    plt.axis('equal')  
    plt.tight_layout()
    plt.show()
 
PREVIOUS NEXT
Tagged: #Nested #pie #chart #graphing #function #put #legend #subplot
ADD COMMENT
Topic
Name
5+8 =