Skip to content

Instantly share code, notes, and snippets.

@abhishek-shrm
Created April 10, 2020 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhishek-shrm/d0f36e69fc0a39067e6f700b1268b4f2 to your computer and use it in GitHub Desktop.
Save abhishek-shrm/d0f36e69fc0a39067e6f700b1268b4f2 to your computer and use it in GitHub Desktop.
# Importing wordcloud for plotting word clouds and textwrap for wrapping longer text
from wordcloud import WordCloud
from textwrap import wrap
# Function for generating word clouds
def generate_wordcloud(data,title):
wc = WordCloud(width=400, height=330, max_words=150,colormap="Dark2").generate_from_frequencies(data)
plt.figure(figsize=(10,8))
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.title('\n'.join(wrap(title,60)),fontsize=13)
plt.show()
# Transposing document term matrix
df_dtm=df_dtm.transpose()
# Plotting word cloud for each product
for index,product in enumerate(df_dtm.columns):
generate_wordcloud(df_dtm[product].sort_values(ascending=False),product)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment