dagrun
dagrun

Reputation: 651

How to generate wordclouds next to each other in Python

I have the following code that generates t figures with wordclouds:

    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()

How can I change this to generate one figure with multiple plots in the same figure?

Upvotes: 2

Views: 5566

Answers (1)

dagrun
dagrun

Reputation: 651

I managed to solve my problem using subplots and the following code:

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()

Here n_components are the number of plots I want to see and also the number of different topics in my topic model. Top_words are the top Words from each topic in my topic model And tile is the title i want for my figure

This code display 4 plots in each row.

Upvotes: 5

Related Questions