How to add WordCloud graph in Streamlit

I want to add the wordcloud to streamlit app to show thw related words in the tweets.

1 Like

Great question and you absolutely can do this with st.pyplot() and wordcloud. Here’s a simple example:

import streamlit as st
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# Create some sample text
text = 'Fun, fun, awesome, awesome, tubular, astounding, superb, great, amazing, amazing, amazing, amazing'

# Create and generate a word cloud image:
wordcloud = WordCloud().generate(text)

# Display the generated image:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
st.pyplot()

Let me know if that works for you!

7 Likes

Hi!

I’m trying to plot 3 word cloud (from WordCloud) figures side by side using subplots from matplotlib.

The app only plots the second and third graphs. That also happens if I add a fourth graph – it will not print the first figure.

I’m fairly new to using all of these packages. Can someone point out my mistake? Thanks a lot!

Here’s the code for 3 charts:

# Plot1
plt.title('Topic '+str(first_choice))
plt.axis('off')
plt.tight_layout()
plt.imshow(wordcloud1)
plt.subplot(1,3,1)

# Plot2
plt.title('Topic '+str(second_choice))
plt.axis('off')
plt.tight_layout()
plt.imshow(wordcloud2)
plt.subplot(1,3,2)

# Plot3
plt.title('Topic '+str(third_choice))
plt.axis('off')
plt.tight_layout()
plt.imshow(wordcloud3)
plt.subplot(1,3,3)

st.pyplot()

And the resulting plots – for 3 and 4 charts:

Hi @csbrl, you can use st.beta_columns to display 3 plots side by side, no need for subplots.

col1, col2, col3 = st.beta_columns(3)
with col1:
    st.header("A cat")
    st.image("https://static.streamlit.io/examples/cat.jpg", use_column_width=True)
with col2:
    st.header("A dog")
    st.image("https://static.streamlit.io/examples/dog.jpg", use_column_width=True)
with col3:
    st.header("An owl")
    st.image("https://static.streamlit.io/examples/owl.jpg", use_column_width=True)

For more information, follow this documentation link

Hi, Amanda! I tried to use this code that you shared, but it didn’t work for me, it shows “TypeError: expected string or bytes-like object”. Do you know how to fix it?