简体   繁体   中英

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:

在此处输入图像描述

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:

在此处输入图像描述

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()

外圈带有标签的馅饼

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM