列表超出WordCloud中LDA mod的范围

2024-06-28 19:07:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的LDA结果创建一个单词云。在编码中,对于以下行:

fig, axes = plt.subplots(1, 2, figsize=(10,10), sharex=True, sharey=True)

我试着改成(9,2),然后打印出所有18个主题的单词cloud,但其中只有10个可以显示,而Jupiter笔记本显示的列表超出了范围。我不知道发生了什么事。如果有人能帮助我,那就太好了!非常感谢你

from matplotlib import pyplot as plt
from wordcloud import WordCloud, STOPWORDS
import matplotlib.colors as mcolors

cols = [color for name, color in mcolors.TABLEAU_COLORS.items()]  #more colors: 'mcolors.XKCD_COLORS'

cloud = WordCloud(stopwords=stop_words,
              background_color='white',
              width=1000,
              height=800,
              max_words=10,
              colormap='tab10',
              color_func=lambda *args, **kwargs: cols[i],
              prefer_horizontal=1.0)

topics = lda_model.show_topics(formatted=False)

fig, axes = plt.subplots(1, 2, figsize=(10,10), sharex=True, sharey=True)

for i, ax in enumerate(axes.flatten()):
    fig.add_subplot(ax)
    topic_words = dict(topics[i][1])
    cloud.generate_from_frequencies(topic_words, max_font_size=300)
    plt.gca().imshow(cloud)
    plt.gca().set_title('Topic ' + str(i), fontdict=dict(size=16))
    plt.gca().axis('off')


plt.subplots_adjust(wspace=0, hspace=0)
plt.axis('off')
plt.margins(x=0, y=0)
plt.tight_layout()
plt.show()

Tags: fromimporttruecloudfigplt单词color