【问题标题】:How to put values inside the outer pie plot in nested pie plot in matplotlib如何将值放在matplotlib的嵌套饼图中的外部饼图中
【发布时间】:2021-04-20 17:18:56
【问题描述】:

数据:

import numpy as np
import matplotlib.pyplot as plt

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

我正在使用上述数据绘制嵌套饼图:

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

以上代码的输出:

我的问题是:

从上图我们可以看到,在内部饼图中,值(%)也在图中,但它不在外部图中。

那么我该怎么做呢?

预期输出:

【问题讨论】:

    标签: python matplotlib data-visualization


    【解决方案1】:

    您要查找的参数是pctdistance。正如pie documentation 注释,默认为0.6。因为你有一个size=0.3radius=1 的外环,这导致标签被放置在内部区域。此处的示例通过指定 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多