- 帖子
- 9
- 精华
- 0
- 积分
- 34
- 阅读权限
- 10
- 注册时间
- 2018-5-18
- 最后登录
- 2018-12-9
|
版主你好, 按‘【编程课堂】词云 wordcloud’ 原文 代码,只改了读取txt及png文件地址,结果就出现以下错误:
输入:
# -*- coding: utf-8 -*-
from scipy.misc import imread
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba
from collections import Counter
# 读入 西游记 txt 文件,windows 下过滤编码错误
text = open('D:/西游记.txt',encoding='utf-8',errors='ignore').read()
# 使用 jieba 分词
text_jieba = list(jieba.cut(text))
# 使用 counter 做词频统计,选取出现频率前 100 的词汇
c = Counter(text_jieba)
common_c = c.most_common(100)
# 读入图片
bg_pic = imread('D:/Anne_Hathaway.png')
# 配置词云参数
wc = WordCloud(
# 设置字体
font_path = '李旭科书法1.4.ttf',
# 设置背景色
background_color='white',
# 允许最大词汇
max_words=200,
# 词云形状
mask=bg_pic,
# 最大号字体
max_font_size=100,
)
# 生成词云
wc.generate_from_frequencies(dict(common_c))
# 生成图片并显示
plt.figure()
plt.imshow(wc)
plt.axis('off')
plt.show()
# 保存图片
wc.to_file('anne.jpg')
输出:
runfile('C:/Users/usb/.spyder-py3/temp.py', wdir='C:/Users/usb/.spyder-py3')
Traceback (most recent call last):
File "<ipython-input-4-4cdbcda25942>", line 1, in <module>
runfile('C:/Users/usb/.spyder-py3/temp.py', wdir='C:/Users/usb/.spyder-py3')
File "d:\Users\usb\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "d:\Users\usb\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/usb/.spyder-py3/temp.py", line 39, in <module>
wc.generate_from_frequencies(dict(common_c))
File "d:\Users\usb\Anaconda3\lib\site-packages\wordcloud\wordcloud.py", line 439, in generate_from_frequencies
font = ImageFont.truetype(self.font_path, font_size)
File "d:\Users\usb\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 238, in truetype
return FreeTypeFont(font, size, index, encoding)
File "d:\Users\usb\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 127, in __init__
self.font = core.getfont(font, size, index, encoding)
OSError: cannot open resource
用的是spyder编译器,python3.6环境。
网上没找到对应的情况,也许搜索方法不对 ?
麻烦帮忙看看问题出在哪儿,谢谢!
|
|