【问题标题】:How to plot a pie chart without overlapping labels on each other如何绘制饼图而不相互重叠标签
【发布时间】:2021-08-30 08:41:14
【问题描述】:

我在绘制饼图时遇到问题。代码如下:

import numpy as np
import matplotlib.pyplot as plt
from collections import Counter

count_legalgroups = Counter(data['LegalFormGroup']).most_common()

for_dict = dict(count_legalgroups)

    
legal_from_list = list(for_dict.keys())

count_from_list = list(for_dict.values())


slices = np.array(count_from_list)

activities = np.array(legal_from_list)

colors = ['yellowgreen','red','gold','lightskyblue','lightcoral','blue','pink', 
'darkgreen','grey','violet','magenta','cyan', 'brown']

patches, texts = plt.pie(slices,
   colors=colors,
   startangle=90,
   labels=activities)

labels = ['{0} - {1:1.2f} %'.format(i, j) for i, j in zip(activities,
100.*slices/slices.sum())]
plt.legend(patches, labels, loc='center left', bbox_to_anchor=(-0.35, .5), fontsize=8)
plt.show()

我得到了这张图片:

我希望它是这样的:

有什么解决办法吗?我错过了什么?

【问题讨论】:

  • 请添加您的数据,然后我们可以运行您的代码并进行调试
  • 对不起,我无法显示数据

标签: python pandas matplotlib pie-chart


【解决方案1】:

检查这个对你有没有帮助,设置大的figsize:

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

figure(figsize = (20, 16), dpi = 80)
...
plt.show()

【讨论】:

  • 正如我所见,没有重叠就无法显示图表,因为值之间存在巨大差异,例如 130 000、65 000、12、5、2 等,并且饼图大小合适对于那个值。
猜你喜欢
  • 2016-02-26
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多