Pie chart showing the three characters played

import jieba
from wordcloud import WordCloud
from scipy.misc import imread
img = imread('china.jpg')
excludes = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",
            "如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",
            "东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",
            '玄德曰', '孔明曰', '刘备', '关公'}
with open('./threekingdom.txt', 'r', encoding='utf-8') as f:
    txt = f.read()
words = jieba.lcut(txt)
counts = {}
for word in words:
    if len(word) == 1:
        continue
    else:
        counts[word] = counts.get(word, 0) + 1

counts['孔明'] = counts.get('孔明') + counts.get('孔明曰')
counts['玄德'] = counts.get('玄德') + counts.get('玄德曰')
counts['玄德'] = counts.get('玄德') + counts.get('刘备')
counts['云长'] = counts.get('云长') + counts.get('关公')



for word in excludes:
    del counts[word]

print(counts)

items = list(counts.items())

items.sort(key=lambda x: x[1], reverse=True)


data, labels = [], []
for i in range(10):
    word, count = items[i]
    data.append(count)
    labels.append(word)
print(data)
print(labels)
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 偏离圆心的距离
explode = [0.2,0,0,0,0,0,0,0,0,0]
plt.pie(data, labels=labels,explode=explode, shadow=True, autopct='%.1f%%')
plt.savefig('./yee.jpg')
plt.show()

Guess you like

Origin blog.csdn.net/weixin_34293902/article/details/90929364