大数据分析,使用python生成词云图时,发生一下错误:
import jieba
from wordcloud import WordCloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
with open('Dream_of_the_Red_Mansion.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 对文本进行分词
words = jieba.cut(text)
result = " ".join(words)
# 生成词云图
image_background = Image.open('Red.jpg')
MASK = np.array(image_background)
wordcloud = WordCloud(font_path='SimHei.ttf',background_color='white', width=4000,
height=2000, margin=10, max_words=200, mask=MASK).generate(result)
plt.imshow(wordcloud)
plt.show()
wordcloud.to_file('final.png')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-eaad86f251cc> in <module>
16 MASK = np.array(image_background)
17
---> 18 wordcloud = WordCloud(font_path='SimHei.ttf',background_color='white', width=4000,
19 height=2000, margin=10, max_words=200, mask=MASK).generate(result)
20
~/.local/lib/python3.8/site-packages/wordcloud/wordcloud.py in generate(self, text)
637 self
638 """
--> 639 return self.generate_from_text(text)
640
641 def _check_generated(self):
~/.local/lib/python3.8/site-packages/wordcloud/wordcloud.py in generate_from_text(self, text)
619 """
620 words = self.process_text(text)
--> 621 self.generate_from_frequencies(words)
622 return self
623
~/.local/lib/python3.8/site-packages/wordcloud/wordcloud.py in generate_from_frequencies(self, frequencies, max_font_size)
451 font_size = self.height
452 else:
--> 453 self.generate_from_frequencies(dict(frequencies[:2]),
454 max_font_size=self.height)
455 # find font sizes
~/.local/lib/python3.8/site-packages/wordcloud/wordcloud.py in generate_from_frequencies(self, frequencies, max_font_size)
506 font, orientation=orientation)
507 # get size of resulting text
--> 508 box_size = draw.textbbox((0, 0), word, font=transposed_font, anchor="lt")
509 # find possible places using integral image:
510 result = occupancy.sample_position(box_size[3] + self.margin,
AttributeError: 'ImageDraw' object has no attribute 'textbbox'
请问该如何解决?