import jieba import numpy from PIL import Image from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt with open("comments.txt","r",encoding="utf-8") as f: comments = f.read() words = jieba.cut(comments) stopwords = set(STOPWORDS) stopwords.add('腾讯') stopwords.add('游戏') filtered_words = [] for word in words: if word not in stopwords: filtered_words.append(word) cloud = WordCloud( font_path="C:\Windows\Fonts\simsun.ttc", background_color="white", mask= ).generate() cloud.to_file("词云.png") print("词云绘制成功")这个代码缺什么
时间: 2024-02-14 07:06:43 浏览: 90
这个代码缺少一个参数,即 WordCloud 类的 mask 参数,用于指定词云的形状。你需要指定一个 PIL Image 对象作为 mask,例如:
```python
mask = np.array(Image.open("mask.png"))
cloud = WordCloud(
font_path="C:\Windows\Fonts\simsun.ttc",
background_color="white",
mask=mask
).generate(" ".join(filtered_words))
```
其中,"mask.png" 是你要指定的形状文件的路径。此外,你需要将筛选后的词列表 `filtered_words` 用空格连接成一个字符串,作为 WordCloud 的输入。同时还需要将生成的词云保存到文件中,你可以指定保存的文件名,例如:
```python
cloud.to_file("词云.png")
```
这样就可以将生成的词云保存到当前目录下的 "词云.png" 文件中了。
相关问题
import jieba import numpy import numpy as np from PIL import Image from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt with open("comments.txt","r",encoding="utf-8") as f: comments = f.read() words = jieba.cut(comments) stopwords = set(STOPWORDS) stopwords.add('腾讯') stopwords.add('游戏') filtered_words = [] for word in words: if word not in stopwords: filtered_words.append(word) mask = np.array(Image.open("mask.png")) cloud = WordCloud( font_path="C:\Windows\Fonts\simsun.ttc", background_color="white", mask=mask ).generate(" ".join(filtered_words)) cloud.to_file("词云.png") print("词云绘制成功")这个代码怎么运行错误
这段代码的错误可能是缩进不正确。正确的代码应该是这样的:
```
import jieba
import numpy
import numpy as np
from PIL import Image
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
with open("comments.txt","r",encoding="utf-8") as f:
comments = f.read()
words = jieba.cut(comments)
stopwords = set(STOPWORDS)
stopwords.add('腾讯')
stopwords.add('游戏')
filtered_words = []
for word in words:
if word not in stopwords:
filtered_words.append(word)
mask = np.array(Image.open("mask.png"))
cloud = WordCloud(
font_path="C:\Windows\Fonts\simsun.ttc",
background_color="white",
mask=mask
).generate(" ".join(filtered_words))
cloud.to_file("词云.png")
print("词云绘制成功")
```
请检查一下你的代码是否与这段代码一致。
修改代码import jieba import numpy as np import matplotlib.pyplot as plt text = open('comment.txt', encoding='utf-8').read() from PIL import Image, ImageDraw, ImageFont from wordcloud import WordCloud text = ' '.join(jieba.cut(text)) # 生成对象 mask = np.array(Image.open("computer.jpg")) wc = WordCloud(ImageFont.truetype("arial.ttf")).generate(text) # 显示词云 # plt.imshow(wc, interpolation = 'bilinear') # plt.axis("off") # plt.show() # 保存文件 wc.to_file('rs_computer.png')
这段代码的问题在于 `WordCloud` 类的实例化时,使用了错误的参数格式。`WordCloud` 类的构造函数需要传入一个字典类型的参数,用于指定词云生成时的各种参数。而在这段代码中,只传入了一个 `ImageFont.truetype("arial.ttf")` 参数,这是错误的。
下面是修改后的代码,已经添加了 `WordCloud` 类的参数字典:
```python
import jieba
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
from wordcloud import WordCloud
text = open('comment.txt', encoding='utf-8').read()
text = ' '.join(jieba.cut(text))
# 生成对象
mask = np.array(Image.open("computer.jpg"))
wc = WordCloud(
font_path=ImageFont.truetype("arial.ttf"),
mask=mask,
background_color="white",
max_words=2000,
max_font_size=100,
width=1600,
height=800,
random_state=42
).generate(text)
# 显示词云
# plt.imshow(wc, interpolation='bilinear')
# plt.axis("off")
# plt.show()
# 保存文件
wc.to_file('rs_computer.png')
```
在这个例子中,我们使用了 `WordCloud` 类的参数字典,指定了生成词云时的各种参数,例如字体文件路径、遮罩图像、背景颜色、最大单词数、最大字体大小、词云图像宽度和高度等。这样就可以正确地生成和保存词云图像了。
阅读全文