2015-12-08 38 views
1

我正在使用包WordCloud顯示由scikit LDA(潛在Dirichlet分配)生成的單詞。對於由LDA生成的每個主題,我都會有一個圖表。我希望能夠繪製網格中的所有圖表以允許並排顯示。 基本上我已經接受一個LDA模型作爲輸入,與LDA話題我想可視化,然後沿着一個函數繪製一個wordcloud:在matplotlib中並排Wordclouds

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 

回答

2

返回從功能wordcloud,然後調用從topicWordCloud中的for循環。然後,使用您使用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')