pythonwordcloudmask_python通过wordcloud绘制词云python3下绘制定制化词云,预先安装wordcloud,准备任意⼀张图⽚eg.jpg,以及⼀个写有⽂案的txt⽂件。
code如下:
#!/user/bin/python
# -*- coding:utf-8 -*-
_author_ = 'xisuo'
import os
import numpy as np
from wordcloud import WordCloud,STOPWORDS
汽车拉力赛
import matplotlib.pyplot as plt
from PIL import Image
def create_wordcloud(docs=None,imgs=None,filename=None):
雷克萨斯es350图片
东莞网上选车牌号''':param docs:读⼊词汇txt,尽量不重复:param imgs: 读⼊想要⽣成的图形,⽹上随便:param filename: 保存图⽚⽂件名:return:'''
# Read the whole text.
current_file = os.getcwd()
text = open(os.path.join(current_file, docs)).read()
alice_mask = np.array(Image.open(os.path.join(current_file, imgs)))中原汽车网
font_path = 'Arial f'
wc = WordCloud(background_color="white",
max_words=2000,
font_path=font_path, # 设置字体格式,如不设置显⽰不了中⽂
mask=alice_mask,
stopwords=STOPWORDS.add("said")
)
# generate word cloud
# store to file风神s30价格
if filename is None:filename="词云结果.png"
<_file(os.path.join(current_file, filename))
docs='' #读⼊的⽂本
imgs="eg.png" #需要绘制的图像
filename='词云结果.png' #保存图⽚⽂件名
create_wordcloud(docs=docs,imgs=imgs,filename=filename)
print('create wordcloud successful')
gtr r34常见问题
在linux/MAC环境下,中⽂字体可能会乱码,原因是缺少中⽂字体或字体集不对,需要重新制定
```终端fc-list :lang=zh
可能会出现-bash: fc-list: command not found错误
需要预装fontconfig
$ sudo apt-get install fontconfig fc-list :lang=zh [output] Arial f
此时,调整.py脚本中的font_path = 'Arial f'内容即可
### 内容
ALPINA B4,DS 4S,DS 5,DS 5LS,DS 6,DS 7,SAVANA,YUKON, ⼤切诺基,⼤指挥官,指南者,指挥官,
牧马⼈,⾃由侠,⾃由光,MINI, MINI CLUBMAN,MINI COUNTRYMAN,鑫源X30LEV, SWM斯威G01,SWM斯威X3,SWM斯威X7,WEY P8,WEY VV5, WEY VV6,WEY
VV7,nan,smart forfour,smart fortwo,佳宝V80, 佳宝V80新能源,夏利,夏利N5,夏利N7,威志V5,森雅R7,森雅R9, 骏派A50,骏派A70,骏派A70E,骏派CX65,骏派D60,骏派D80, 劲炫ASX,君阁,奕歌,帕杰罗·劲畅,欧蓝德,翼神,蓝瑟,上汽⼤通D90, 上汽⼤通G10,上汽⼤通G20,上汽⼤通G50,V3菱悦,V6菱仕, 东南A5翼舞,东南DX3,东南DX7,俊风,俊风E11K,俊风E17, 俊风ER30,帅客,御风,东风⼩康C35,东风⼩康K01,东风风光, 东风风光580,东风风度MX5,东风风度MX6,东风A9,东风风神A30, 东风风神A60,东风风神AX3,东风风神AX4,东风风神AX5, 东风风神AX7,东风风神H30,东风风神L60,景逸,景逸S50, 景逸X3,景逸X5,景逸X6,菱智,菱智M5EV,风⾏CM7,风⾏F600 ```
eg.jpg链接:
结果如下: