【问题标题】:Plotting two nltk freqdists绘制两个 nltk freqdist
【发布时间】:2018-10-20 22:33:14
【问题描述】:

我一直在关注可在此处找到的样式学教程 (programminghistorian.com)。这使用 matplotlib 来绘制一些文本的频率分布。相关代码如下:

for author in authors:
tokens = nltk.word_tokenize(federalist_by_author[author])

# Filter out punctuation
federalist_by_author_tokens[author] = ([token for token in tokens
                                        if any(c.isalpha() for c in token)])

# Get a distribution of token lengths
token_lengths = [len(token) for token in federalist_by_author_tokens[author]]
federalist_by_author_length_distributions[author] = nltk.FreqDist(token_lengths)
federalist_by_author_length_distributions[author].plot(15, title=author)

不幸的是,尽我所能,我似乎无法将这些分布覆盖到同一个 pyplot 上 - 使用此代码只会为每个作者一次打开一个新图,而不是通常的 matplotlib 'plt .plot()' 将它们添加到同一个 pyplot 的行为,这是我想要的。

关于如何做到这一点的任何想法?

【问题讨论】:

    标签: python matplotlib nltk


    【解决方案1】:

    类似于几个小时前问过的this question,您需要通过在交互模式下绘图来欺骗nltk 函数的show() 使其不生效:

    # turn interactive on
    plt.ion()
    # your code :
    for foo in bars:
        frqdst = nltk.FreqDist(...)
        frqdst.plot(...)
    # turn interactive off
    plt.ioff()
    plt.show()
    

    【讨论】:

      【解决方案2】:

      我没有看到任何in the source of FreqDist 会强制打开新窗口的内容。 (让我们暂时忽略源使用pylab 而不是pyplot 没有充分理由;这是一个非常糟糕的做法)。

      我怀疑发生了什么是最后的pylab.show() 调用弹出带有第一个图的图形窗口,并阻塞直到第一个图形关闭。如果是这种情况,在开始时调用 plt.ion() 以启用交互模式可能会使对 show() 的调用成为非阻塞的,并且您将得到与预期相同的单个图形。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-05
        • 1970-01-01
        • 2012-04-19
        • 1970-01-01
        • 2023-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多