一、代码
from wordcloud import WordCloud
import pandas as pd
import matplotlib.pyplot as plt
from nltk.tokenize import word_tokenize
def remove_stop_words(f):
stop_words = ['Movie']
for stop_word in stop_words:
f = f.replace(stop_word, '')
return f
def create_word_cloud(f):
print('根据词频,开始生成词云!')
f = remove_stop_words(f)
cut_text = word_tokenize(f)
cut_text = " ".join(cut_text)
wc = WordCloud(
max_words=100,
width=2000,
height=1200,
)
wordcloud = wc.generate(cut_text)
wordcloud.to_file("wordcloud.jpg")
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
data = pd.read_csv('movies.csv')
title = " ".join(data['title'])
print("data['title']:", data['title'])
genres = " ".join(data['genres'])
all_word = title + genres
create_word_cloud(all_word)
二、生成效果
