饼图标签重叠

时间:2020-05-04 19:47:33

标签: python numpy matplotlib

我在生成与百分比和国家/地区代码均不与文本重叠的图形时遇到问题,我正在使用python3 matplotlib,请参见下图。

任何解决此问题的方法都可以使用,即使它更改了布局。

from collections import Counter
import numpy as np
import matplotlib.pyplot as plt
import json
countries = []
import os

path = 'data_used_for_graph'

entries = os.listdir(path)
for file in entries:
    with open(path+"/"+file) as f:
        content = json.load(f)
        for x in content['personalNames']:
                countries.append(x['countryOrigin'])

counts = Counter(countries)

labels, values = zip(*counts.items())

# sort your values in descending order
indSort = np.argsort(values)[::-1]

# rearrange your data
labels = np.array(labels)[indSort]
values = np.array(values)[indSort]


# Data to plot
sizes = values

# Plot
plt.pie(sizes, labels=labels,autopct='%1.1f%%', shadow=True, startangle=140)
plt.show()

enter image description here

0 个答案:

没有答案