matplotlib 中的并排词云
Side by side Wordclouds in matplotlib
我正在使用包 WordCloud to display words generated by scikit LDA(潜在狄利克雷分配)。对于 LDA 生成的每个主题,我都会有一个图表。我希望能够在网格中绘制所有图表,以便并排可视化。
本质上,我有一个将 LDA 模型作为输入的函数,以及我想要可视化的 LDA 主题,然后绘制一个词云:
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
Return 来自函数的词云,然后从 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')
我正在使用包 WordCloud to display words generated by scikit LDA(潜在狄利克雷分配)。对于 LDA 生成的每个主题,我都会有一个图表。我希望能够在网格中绘制所有图表,以便并排可视化。 本质上,我有一个将 LDA 模型作为输入的函数,以及我想要可视化的 LDA 主题,然后绘制一个词云:
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
Return 来自函数的词云,然后从 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')