简体   繁体   English

如何将值放入外部饼图 plot 中的嵌套饼图 plot 中的 matplotlib

[英]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:我正在使用上述数据绘制嵌套饼图 plot:

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:上述代码的Output:

在此处输入图像描述

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.从上图中我们可以看到,在内部饼图 plot 中,值(%)也在 plot 内部,但不在外部 plot 内部。

So how can I do this?那么我该怎么做呢?

Expected Output:预期 Output:

在此处输入图像描述

The parameter you are looking for is pctdistance .您正在寻找的参数是pctdistance As the pie documentation notes, the default is 0.6 .正如pie 文档所述,默认值为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.因为您有一个size=0.3radius=1的外环,所以这会导致标签被放置在内部区域中。 The example here centers the labels in the center of the outer rings by specifying pctdistance .此处的示例通过指定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()

外圈带有标签的馅饼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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