Python NLTK:模态频率条形图

Python NLTK:模态频率条形图,python,nltk,Python,Nltk,我是nltk的初学者。最近,我在绘制情态动词频率条形图时遇到了困难 colors = 'rgbcmyk' def bar_chart(categories, words, counts): import pylab ind = pylab.arange(len(words)) width = 1 / (len(categories) + 1) bar_groups = [] for c in range(len(categories)):

我是nltk的初学者。最近,我在绘制情态动词频率条形图时遇到了困难

colors = 'rgbcmyk'
def bar_chart(categories, words, counts):
    import pylab
    ind = pylab.arange(len(words))
    width = 1 / (len(categories) + 1)
    bar_groups = []
    for c in range(len(categories)):
        bars = pylab.bar(ind+c*width, counts[categories[c]], width, 
                         color=colors[c % len(colors)])
        bar_groups.append(bars)
    pylab.xticks(ind+width, words)
    pylab.legend([b[0] for b in bar_groups], categories, loc = 'upper left')
    pylab.ylabel('Frequency')
    pylab.title('Frequency of Six Modal Verbs by Genre')
    pylab.show()

import nltk
from nltk.corpus import brown
genres = ['news', 'religion', 'hobbies', 'government', 'adventure']
modals = ['can', 'could', 'may', 'might', 'must', 'will']
cfd = nltk.ConditionalFreqDist(
        (genre, word)
        for genre in brown.categories()
        for word in brown.words(categories = genre)
        if word in modals)
counts = {}
for genre in genres:
    counts[genre] = [cfd[genre][word] for word in modals]
bar_chart(genre, modals, counts)
Python可以在运行函数“bar_chart”后提供该格式,但无法看到该条。我怀疑Python是否没有从brown读取数据,因此我使用:

cfd.tabulate(conditions = genres, samples = modals)
输出:


看起来Python读取了数据。我想确定错误在哪里。非常感谢。

我想知道您是否正在尝试从cmdline或某个IDE运行它。在后一种情况下,显示图表可能会被IDE阻止。试试命令行。

我总是讨厌处理条形图,并尽可能多地提取工作。一种方法是使用将数据加载为数据框,然后使用其打印界面(使用matplotlib)创建条形图

因此,您可以去掉
条形图
函数,执行如下操作:

import pandas as pd

df = pd.DataFrame(list(counts.values()), counts.keys(), modals)
df.plot(kind='bar')
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
df.plot(kind='bar', ax=ax)
plt.tight_layout()
plt.savefig('some file name.png')
也就是说,显示图像的困难实际上取决于您工作的环境。如果您在Jupyter中,可以使用magic命令
%matplotlib inline
,调用plot方法后,图像将立即弹出。如果正在编写脚本并希望保存图像,可以执行以下操作:

import pandas as pd

df = pd.DataFrame(list(counts.values()), counts.keys(), modals)
df.plot(kind='bar')
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
df.plot(kind='bar', ax=ax)
plt.tight_layout()
plt.savefig('some file name.png')
你应该以这样的方式结束:

是的,我解决了这个难题。非常感谢你。