python - python,nltk,单词频率的多重绘图结果(子图)
问题描述
我正在尝试将每个文本中的 most_common(10) 单词可视化。这是我的代码。
(我使用 nltk.corpus.gutenberg 所有文本)
from nltk.corpus import gutenberg
import matplotlib.pyplot as plt
from nltk import ConditionalFreqDist
from nltk.corpus import stopwords
%matplotlib inline
stop_words = set(stopwords.words('English'))
cfd = ConditionalFreqDist((target[:-4], word.lower())
for target in gutenberg.fileids()
for word in gutenberg.words(target)
if word.lower() not in stop_words
and word.isalpha())
plt.figure(figsize = (10,10))
for i, title in enumerate(list(cfd.keys())):
plt.subplot(6,3,i+1)
cfd[title].plot(10)
我想使用 subplot 绘制 6x3,但结果始终是 18x1。有什么好的方法吗?谢谢。
解决方案
我能够使用 subplot2grid 的方式是将 plt.subplot() 调用替换为 subplot2grid 调用。以下是我如何让它工作:
plt.figure(figsize = (6,3))
for i, title in enumerate(list(cfd.keys())):
# plt.subplot(6,3,i+1)
plt.subplot2grid((6,3),(0,0), rowspan=10, colspan=10)
cfd[title].plot(10, title=title)
这是 subplot2grid 示例的一般用途: # 用不同大小的子图绘制图形 fig = plt.figure(1) # 设置子图网格 gridspec.GridSpec(3,3)
# large subplot
plt.subplot2grid((3,3), (0,0), colspan=2, rowspan=3)
推荐阅读
- jinja2 - 如何在 BashOperator 中执行 python 代码和气流宏?
- php - imap_open 很长的延迟
- javascript - 使消息返回所有数据库行
- php - 使用 PHP 加载外部站点而不破坏该站点中的相关链接
- angular - 根据角度 6 中的输入文本字段值创建 x 个子组件
- java - 如何将输入的社会安全号码附加到 xxx-xx-1111?
- r - 我想了解为什么 lapply 会耗尽内存但 for 循环不会
- postgresql - sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)无法连接到服务器:连接被拒绝
- node.js - 标头未定义
- python-3.x - 如何通过从上到下的分层多索引循环熊猫数据框分组并存储结果