我一直在跟踪这里可以找到的测定仪教程(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中的行为--这正是我想要的。
对怎么做有什么想法吗?
发布于 2018-10-20 23:05:02
类似于几个小时前被问到的这个问题,您需要欺骗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()发布于 2018-10-20 22:40:16
我没有看到任何FreqDist会强行打开一个新窗口。(现在让我们忽略一下,源代码使用pylab而不是pyplot是没有充分理由的;这是一个非常糟糕的实践)。
我怀疑最终的pylab.show()调用会弹出带有第一个图形的图形窗口,并阻塞直到第一个图形关闭。如果是这样的话,在开始时调用plt.ion()以启用交互模式可能会调用show()非阻塞模式,并且您将得到与预期相同的图形。
https://stackoverflow.com/questions/52910655
复制相似问题