简体   繁体   中英

How to view and save the WordCloud generated in Python

I am trying to generate a WordCloud in Python from a .csv file.

This is how my .csv file looks like:

title,views
universe,45678
earth,34366
smtp,4567987

This is what I did to create the WordCloud :

import csv
import matplotlib.pyplot as pPlot
from wordcloud import WordCloud, STOPWORDS

reader = csv.reader(open('C:/Users/Sushma/Downloads/wordcloud.csv', 'r', newline = '\n', encoding = 'utf-8'))

for k, v in reader:
    d[k] = float(v)

wordcloud = WordCloud().generate_from_frequencies(d)

If I print WordCloud , I get an object id .

Can anybody please suggest how to save this as an image and how to view it on Python?

First, there are some errors in your code:

1. The .csv file must be placed in a list for reading:

with open('C:/Users/Sushma/Downloads/wordcloud.csv', 'r', encoding = 'utf8', newline = '\r\n') as file:
    reader = csv.reader(file, skipinitialspace = False, delimiter = ',', quoting = csv.QUOTE_NONE)
    data = list(reader)

2. After this, a dictionary must be created that contains the key and the value in int format so that later, we can create the WordCloud :

word_freq = {}
for k, v in data[1:]:
   word_freq[k] = int(v)

Now, you need to create a variable that will receive the multiplication between the text and its frequency :

text = ' '.join([(k + ' ') * v for k, v in word_freq.items()])

Then, you will create the WordCloud that will receive this variable text :

wordcloud = WordCloud(width = 1600,
                      height = 800,
                      background_color = 'white',
                      collocations = False,
                      repeat = True).generate_from_text(text)

plt.figure(figsize=(10,5))
plt.imshow(wordcloud, interpolation = "bilinear")
plt.axis('off')

And now you can simply save the generated WordCloud :

plt.savefig(f'wordcloud.png',
            dpi = 300)

To plot (visualize) just use:

plt.show()

Final result:

Final result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM