繁体   English   中英

我不知道为什么饼图的标签要写两次

[英]I don't know why labels of pie chart are written twice

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
import seaborn as sns
    
frequency = [35.3, 4.1, 6.9, 1.7, 0.3, 13.4, 7.2, 4.0, 6.5, 13.5, 7.0]
    
font_path = "C:\Windows\Fonts\malgun.ttf"
font = font_manager.FontProperties(fname = font_path).get_name()
rc('font', family = font)
    
places = list(df['비고'])
times = list(df[df.columns[0]])
    
colors = sns.color_palette('hls', len(places))
    
fig1, ax1 = plt.subplots()
fig1.set_facecolor('white')
ax1.pie(frequency, labels = places, autopct='%1.1f%%', startangle=90, counterclock=False, colors= colors)
ax1.axis('equal')
ax1.set_facecolor('white')
    
frequency = [35.3, 4.1, 6.9, 1.7, 0.3, 13.4, 7.2, 4.0, 6.5, 13.5, 7.0]
    
sum_pct = 0
threshold = 5
    
total = np.sum(frequency)
    
bbox_props = dict(boxstyle='square',fc='w',ec='w',alpha=0) 
    
config = dict(arrowprops=dict(arrowstyle='-'),bbox=bbox_props,va='center')
     
for i,l in enumerate(places):
    ang1, ang2 = ax1.patches[i].theta1, ax1.patches[i].theta2 
    center, r = ax1.patches[i].center, ax1.patches[i].r 
        
    if i < len(places) - 1:
        sum_pct += float(f'{frequency[i]/total*100:.2f}')
        text = f'{frequency[i]/total*100:.2f}%'
    else: 
        text = f'{100-sum_pct:.2f}%'
        
        
    if frequency[i]/total*100 < threshold:
        ang = (ang1+ang2)/2 
        x = np.cos(np.deg2rad(ang)) 
        y = np.sin(np.deg2rad(ang)) 
            

        horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
        connectionstyle = "angle,angleA=0,angleB={}".format(ang) 
        config["arrowprops"].update({"connectionstyle": connectionstyle})
        ax1.annotate(text, xy=(x, y), xytext=(1.5*x, 1.2*y),
                    horizontalalignment=horizontalalignment, **config)
    else:
            x = (r/2)*np.cos(np.pi/180*((ang1+ang2)/2)) + center[0]
            y = (r/2)*np.sin(np.pi/180*((ang1+ang2)/2)) + center[1]
            ax1.text(x,y,text,ha='center',va='center',fontsize=12)
        

我正在使用注释来舒适地查看饼图

在此处输入图像描述

如您所见,饼图的标签写了两次

但是,我不知道如何解决这个问题

查看我的代码,请告诉我出了什么问题

如果您需要更多解释,请在下方评论

您专门向 plot 询问pie autopct='%1.1f%%'的百分比:

ax1.pie(frequency, labels=places,
        autopct='%1.1f%%',                                # HERE
        startangle=90, counterclock=False, colors= colors)

暂无
暂无

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

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