- 乱码
wordcloud直接读取中文文件,通常会显示乱码(如下图)
- 添加中文字体
此时我们需要添加中文字体的ttc文件,方法如下:
在finder中找到中文字体的ttc文件,将该路径赋值给font_path即可
(即图片中第二行,各操作系统不一样,具体见下)
根据不同的操作系统,修改font_path赋值(通常):
- MAC
wc = WordCloud(background_color='white',
font_path = '/System/Library/Fonts/Supplemental/Songti.ttc',
width=1000,
height=800,
).generate(string1)
- Windows
wc = WordCloud(background_color='white',
font_path = 'C:\WINDOWS\Fonts\中文字体.ttf',
width=1000,
height=800,
).generate(string1)
- Linux
wc = WordCloud(background_color='white',
font_path = '/usr/share/fonts',
width=1000,
height=800,
).generate(string1)