【问题标题】:Side by side Wordclouds in matplotlibmatplotlib 中的并排 Wordcloud
【发布时间】:2016-03-13 22:02:51
【问题描述】:

我正在使用包WordCloud 来显示由scikit LDA 生成的单词(潜在狄利克雷分配)。对于 LDA 生成的每个主题,我都会有一个图表。我希望能够在网格中绘制所有图表以允许并排可视化。 本质上,我有一个函数,它将 LDA 模型作为输入,以及我想要可视化的 LDA 主题,然后绘制一个 wordcloud:

from wordcloud import WordCloud
import matplotlib.pyplot as plt
SEED=0

def topicWordCloud(model, topicNumber, WCmaxWords,WCwidth, WCheight):
    topic = model.components_[topicNumber]
    tupleList = [(tf_feature_names[i],int(topic[i]/topic.sum()*10000)) for i in range(len(topic))]
    wordcloud = WordCloud(width=WCwidth, height=WCheight, max_words=WCmaxWords, random_state=42).generate_from_frequencies(tupleList)
    plt.figure( figsize=(20,10) )
    plt.imshow(wordcloud)
    plt.axis("off")

topicWordCloud(model=lda, topicNumber=2, WCmaxWords=100,WCwidth=800, WCheight=600)

如何循环浏览我的所有主题 (n_topics) 以可视化网格中的所有图表?我的想法是这样的:

fig = plt.figure()
for i in range(n_topics):
    plt.subplot(2,1,i+1) 
    #something here

【问题讨论】:

    标签: python matplotlib lda word-cloud


    【解决方案1】:

    从你的函数中返回 wordcloud,然后在你的 for 循环中调用 topicWordCloud。然后,在您使用fig.add_subplot 创建的Axes 上使用imshow。例如,像这样:

    def topicWordCloud(model, topicNumber, WCmaxWords,WCwidth, WCheight):
        topic = model.components_[topicNumber]
        tupleList = [(tf_feature_names[i],int(topic[i]/topic.sum()*10000)) for i in range(len(topic))]
        wordcloud = WordCloud(width=WCwidth, height=WCheight, max_words=WCmaxWords, random_state=42).generate_from_frequencies(tupleList)
        return wordcloud
    
    fig = plt.figure()
    for i in range(n_topics):
        ax = fig.add_subplot(2,1,i+1)
        wordcloud = topicWordCloud(...)
    
        ax.imshow(wordcloud)
        ax.axis('off')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-16
      • 2017-11-25
      • 2020-07-25
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      相关资源
      最近更新 更多