1、请确保你的python环境没有问题,用的开发工具是VsCode,首先你要在Python扩展中安装python开发环境(当然,这不是为你的windows安装python)。
2、那么你还需要安装所需要的第三方库,那么在VSCode中并没有PyCharm那么专业,这里需要获得你自己的Python脚本位置。
3、我们可以发现里面有一个名为pip.exe文件,这个文件就是python官方给我们去安装python第三方库的一个程序,那么我们可以在VsCode的终端中就可以去通过它,这也是我们为什么要获取python安装位置的根本原因。
4、python做词云呢,需要导入的包有wordcloud和PIL,其中PIL(Python Image Library)是python平台图像处理标准库,功能是真的强大。首先需要读取文件 。
5、如果python引入无误,并代码无误,那么会弹出你生成的图片,该图片会储存在你的系统。
import numpy as np #数据处理
import matplotlib.pyplot as plt #作图
from wordcloud import WordCloud #词云函数
import jieba #分割中文的包
from imageio import imread #读取图片 ....后面还有根据自己需要安装包
解决办法:在open函数中加上encoding="utf-8"
with open("./xxx.txt",'r',encoding='utf-8')as f:
text=f.read()
f.close()
解决办法:选择一个支持中文显示的字体。如在电脑中C:\Windows\Fonts\选择有个中文的字体,如,font = r'C:\Windows\Fonts\simfang.ttf',后面再使用WordCloud 的参数font_path=font。
几个简单实例:
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud
text = "square" #表示内容
x, y = np.ogrid[:300, :300]
mask = (x - 150) ** 2 + (y - 150) ** 2 >130 ** 2
mask = 255 * mask.astype(int)
wc = WordCloud(background_color="white", repeat=True, mask=mask)
wc.generate(text)
plt.axis("off")
plt.imshow(wc, interpolation="bilinear")
plt.show()
单字内容
import os
from os import path
from wordcloud import WordCloud
# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()
# Generate a word cloud image
wordcloud = WordCloud().generate(text)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
多字的内容,内容从本地电脑中获取
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os
from wordcloud import WordCloud, STOPWORDS
# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
# Read the whole text.
text = open(path.join(d, 'alice.txt')).read()
# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
alice_mask = np.array(Image.open(path.join(d, "alice_mask.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
stopwords=stopwords, contour_width=3, contour_color='steelblue')
# generate word cloud
wc.generate(text)
# store to file
wc.to_file(path.join(d, "alice.png"))
# show
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()
使用图片来做词云
更多信息可以参看wordcloud官网:
https://amueller.github.io/word_cloud/
上面有更多的例子,上面内容也来自于网站整理。
也可参考网站:
https://blog.csdn.net/xiemanR/article/details/72796739?utm_source=blogxgwz7