如何保存nltk FreqDist图? [英] How to save a nltk FreqDist plot?

查看:158
本文介绍了如何保存nltk FreqDist图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了不同的方法来保存我的情节,但是我尝试过的每件事都显示为空白图像,并且我目前还没有想法.有其他建议可以解决此问题吗?代码示例如下.

I've tried different methods to save my plot but every thing I've tried has turned up with a blank image and I'm not currently out of ideas. Any help with other suggestions that could fix this? The code sample is below.

word_frequency = nltk.FreqDist(merged_lemmatizedTokens) #obtains frequency distribution for each token
print("\nMost frequent top-10 words: ", word_frequency.most_common(10))
word_frequency.plot(10, title='Top 10 Most Common Words in Corpus')
plt.savefig('img_top10_common.png')

推荐答案

当我第一次初始化图形对象,然后调用绘图函数并最终保存图形对象时,我能够保存NLTK FreqDist绘图.

I was able to save the NLTK FreqDist plot, when I first initialized a figure object, then called the plot function and finally saved the figure object.

import matplotlib.pyplot as plt
from nltk.probability import FreqDist

fig = plt.figure(figsize = (10,4))
plt.gcf().subplots_adjust(bottom=0.15) # to avoid x-ticks cut-off
fdist = FreqDist(merged_lemmatizedTokens)
fdist.plot(10, cumulative=False)
plt.show()
fig.savefig('freqDist.png', bbox_inches = "tight")

这篇关于如何保存nltk FreqDist图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆