import matplotlib.pyplot as plt
import numpy as np
import nltk
colors = 'rgbcmyk'
def bar_test(categories, words, counts):
ind = np.arange(len(words))
bar_list = []
width = 1/(len(categories)+1)
for category_id in range(len(categories)):
bar = plt.bar(ind + width * category_id,
counts.get(categories[category_id]),
width,
color = colors[category_id % len(colors)])
bar_list.append(bar)
plt.xticks(ind+width*2,words)
plt.legend([b[0] for b in bar_list], categories, loc = 'upper left')
plt.xlabel('words in different categories')
plt.ylabel('counts')
plt.title('word freq')
plt.show()
genres = ['news', 'religion', 'hobbies', 'government', 'adventure']
words = ['can', 'could', 'may', 'might', 'must', 'will']
cfd = nltk.ConditionalFreqDist((genre, word) for genre in genres
for word in nltk.corpus.brown.words(categories = genre)
if word in words)
counts = {}
for genre in genres:
counts[genre] = [cfd[genre][word] for word in words]
bar_test(genres, words, counts)
matplot绘制柱形图
最新推荐文章于 2022-07-01 00:56:21 发布