Super Ultra Noob
Super Ultra Noob

Reputation: 117

How to put values inside the outer pie plot in nested pie plot in matplotlib

Data:

import numpy as np
import matplotlib.pyplot as plt

labels=['Cat','Dog','Human','Rabbit']
data1=[35,80,2,20]
data2=[5,3,80,8]

I was plotting nested pie plot by using the above data:

size=0.3
fig,ax=plt.subplots(figsize=(20,10))
cmap=plt.get_cmap('tab20c')
outer_colors=cmap(np.arange(0,3)*5)
inner_colors=cmap(np.arange(0,3)*5)
ax.pie(x=data1,autopct='%.2f%%',shadow=True,startangle=180,radius=1,wedgeprops={'width':size,'edgecolor':'c'},colors=outer_colors)
ax.pie(x=data2,autopct='%.2f%%',shadow=True,startangle=180,radius=0.5,wedgeprops={'width':size,'edgecolor':'c'},colors=inner_colors)
plt.title('Good vs Bad Pets',fontsize=18,weight='bold')
plt.legend(labels,fontsize=15)
plt.show()

Output of the above code:

enter image description here

My Question is:

As From the above image we can see that in inside pie plot the values(%) are also inside the plot but it is not inside in outer plot.

So how can I do this?

Expected Output:

enter image description here

Upvotes: 2

Views: 777

Answers (1)

gil
gil

Reputation: 81

The parameter you are looking for is pctdistance. As the pie documentation notes, the default is 0.6. Because you have an outer ring of size=0.3 and radius=1, this results in the labels being placed in the inner region. The example here centers the labels in the center of the outer rings by specifying pctdistance.

size=0.3
fig, ax = plt.subplots(figsize=(20,10))
cmap = plt.get_cmap('tab20c')
outer_colors = cmap(np.arange(0,3)*5)
inner_colors = cmap(np.arange(0,3)*5)
wedges, text, autopct=ax.pie(x=data1, autopct='%.2f%%', shadow=True,
                             startangle=180, radius=1,
                             wedgeprops={'width':size, 'edgecolor':'c'},
                             colors=outer_colors, pctdistance=(1-size/2))
ax.pie(x=data2, autopct='%.2f%%', shadow=True, startangle=180, radius=0.5,
       wedgeprops={'width':size, 'edgecolor':'c'}, colors=inner_colors)
plt.title('Good vs Bad Pets',fontsize=18,weight='bold')
plt.legend(labels,fontsize=15)
plt.show()

pie with labels in outer ring

Upvotes: 1

Related Questions