gpt4 book ai didi

python - 绘制两个 nltk 频率分布

转载 作者:行者123 更新时间:2023-12-01 01:30:36 25 4
gpt4 key购买 nike

我一直在关注可以在这里找到的风格测量教程(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 中的行为,这正是我想要的。

关于如何做到这一点有什么想法吗?

最佳答案

类似于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()

关于python - 绘制两个 nltk 频率分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com