【无标题】AttributeError: ‘TransposedFont‘ object has no attribute ‘getbbox‘

Python 运行 Wordcloud的时候出现了这个错误。

from wordcloud import WordCloud
from PIL import Image
import matplotlib.pyplot as plt
topics = model.show_topics(formatted=False)

# 构造单词-概率的字典
word_prob = {}
for topic in topics:
    for word, prob in topic[1]:
        word_prob[word] = prob
        
# 定义 color_func
# def black_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
#     return "rgb(0, 0, 0)"


wc=WordCloud(background_color='white',
            width=600,
            height=400,
            font_path='Arial.ttf',
            #color_func=black_color_func,
            ).generate_from_frequencies(word_prob)

# 绘制词云
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()

######################

代码没有任何问题,font的决定路径也附上了(特别是中文词云一定要加上font_path的参数,可能会乱码)

###################### 

解决方案:

1.多换几个字体文件,以排除文件错误的可能性(文件格式:TrueType(.ttf)、OpenType(.otf)和 PostScript(.pfb/.pfm))

2.路径是否正确

3.升级 Pillow 库:getbbox()方法是ImageFont对象的方法,实际上是在 Pillow 库中实现的。

pip install --upgrade pillow
 

你可能感兴趣的:(python,中文分词)