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)
I am using annotation to see pie chart comfortable
Like what you see, labels of pie chart are written twice
but, i don't know how to solov this problem
see my code and please tell me what is wrong
if you want more explation, please comment under