首页 > 解决方案 > 如何在 Python 中生成彼此相邻的词云

问题描述

我有以下代码使用 wordclouds 生成 t 图形:

    for t in range(n_components):
        plt.figure()
        plt.imshow(WordCloud().fit_words(lda_top_words[t]))
        plt.axis("off")
        plt.title("Topic #" + str(t))
        plt.show()

我怎样才能改变它来生成一个在同一个图中有多个图的图?

标签: pythonmatplotlibplotword-cloud

解决方案


我设法使用子图和以下代码解决了我的问题:

def display_wordcloud(top_words, title, n_components):
    plt.figure()
    j = np.ceil(n_components/4)
    for t in range(n_components):
        i=t+1
        plt.subplot(j, 4, i).set_title("Topic #" + str(t))
        plt.plot()
        plt.imshow(WordCloud().fit_words(top_words[t]))
        plt.axis("off")
    fig.suptitle(title)
    plt.show()

这里的 n_components 是我想看到的图的数量,也是我的主题模型中不同主题的数量。Top_words 是我的主题模型中每个主题的热门词,而 tile 是我想要的人物的标题

此代码每行显示 4 个图。


推荐阅读