Python绘制饼图调节字体大小、防止标签重叠解决方法

  1. 设置字体的大小
patches,l_text,p_text=plt.pie(money_rate,explode=explode,labels=names,autopct='%.2f%%')
# l_text是饼图对着文字大小,p_text是饼图内文字大小
for t in p_text:
    t.set_size(5)

for t in l_text:
    t.set_size(6)
  1. 防止标签重叠,可以将窗口设置的大一些
plt.figure(figsize=(20, 6.5))

3.源码

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

matplotlib.rcParams['font.family']='SimHei'

names=np.array(['正义联盟','东方快车谋杀案','密战','寻梦环游记','雷神3:诸神黄昏', '全球风暴', '降魔传','追捕','七十七天','狂兽','其它'])
money=np.array([57767,22354,7916,15969,73853,14839,8725,8716,8318,6764,52222])
money_rate=money/np.sum(money)
explode=np.zeros((len(money)))
explode[4]=0.1

plt.figure(figsize=(20, 6.5))
patches,l_text,p_text=plt.pie(money_rate,explode=explode,labels=names,autopct='%.2f%%')

plt.legend(["雷神3:诸神黄昏","正义联盟","东方快车谋杀案","寻梦环游记","全球风暴","降魔传","追捕","七十七天","密战","狂兽","其它"])
plt.legend(loc='center right')
plt.title('电影排片占比')
plt.axis('equal')

# 设置饼图内文字大小

for t in p_text:
    t.set_size(5)

for t in l_text:
    t.set_size(6)
plt.show()

4.结果

posted @ 2020-04-29 19:20  梦小冷  阅读(10529)  评论(0编辑  收藏  举报