python信息化技术实践之十一词云效果实战
WordCloud
在File-->Setting,找到项目的解释器选项
安装各个类库,jieba分词器
matplotlib类库
如果安装失败,使用命令行工具安装:
Archived: Python Extension Packages for Windows - Christoph Gohlke (uci.edu)
在Mac下安装wordcloud尝试了各种方法终于安装成功
Mac:版本11.6.5
Python:3.10.2
一、【安装xcode-select】在终端运行指令:
xcode-select --install
如果安装失败,进行如下步骤操作:
1、强制删除安装目录下的文件
sudo rm -rf /Library/Developer/CommandLineTools
2、再次安装
xcode-select --install
1.
二、【安装wordcloud】在终端运行指令:
python3 -m pip install wordcloud
1.
升级pip到最新版本:python3 -m pip install --upgrade pip
WARNING: You are using pip version 21.3.1; however, version 22.3.1 is available.
You should consider upgrading via the '/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10 -m pip install --upgrade pip' command.
~/Downloads » python3 -m pip install --upgrade pip
Requirement already satisfied: pip in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (21.3.1)
Collecting pip
Downloading pip-22.3.1-py3-none-any.whl (2.1 MB)
|████████████████████████████████| 2.1 MB 3.7 kB/s
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 21.3.1
Uninstalling pip-21.3.1:
Successfully uninstalled pip-21.3.1
Successfully installed pip-22.3.1
------------------------------------------------------------
~/Downloads »
安装wordcloud,如果失败多试几次。
~/Downloads » python3 -m pip install wordcloud kongfanyu@kongfanyudeMacBook-Pro
Collecting wordcloud
Using cached wordcloud-1.8.2.2-cp310-cp310-macosx_10_9_x86_64.whl (160 kB)
Collecting matplotlib
Using cached matplotlib-3.6.2-cp310-cp310-macosx_10_12_x86_64.whl (7.3 MB)
Requirement already satisfied: numpy>=1.6.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from wordcloud) (1.24.1)
Requirement already satisfied: pillow in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from wordcloud) (9.3.0)
Collecting fonttools>=4.22.0
Using cached fonttools-4.38.0-py3-none-any.whl (965 kB)
Collecting python-dateutil>=2.7
Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting packaging>=20.0
Downloading packaging-22.0-py3-none-any.whl (42 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.6/42.6 kB 412.8 kB/s eta 0:00:00
Collecting cycler>=0.10
Using cached cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting pyparsing>=2.2.1
Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.3/98.3 kB 77.9 kB/s eta 0:00:00
Collecting kiwisolver>=1.0.1
Downloading kiwisolver-1.4.4-cp310-cp310-macosx_10_9_x86_64.whl (65 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.5/65.5 kB 2.9 kB/s eta 0:00:00
Collecting contourpy>=1.0.1
Using cached contourpy-1.0.6-cp310-cp310-macosx_10_9_x86_64.whl (240 kB)
Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib->wordcloud) (1.16.0)
Installing collected packages: python-dateutil, pyparsing, packaging, kiwisolver, fonttools, cycler, contourpy, matplotlib, wordcloud
Successfully installed contourpy-1.0.6 cycler-0.11.0 fonttools-4.38.0 kiwisolver-1.4.4 matplotlib-3.6.2 packaging-22.0 pyparsing-3.0.9 python-dateutil-2.8.2 wordcloud-1.8.2.2
安装matplotlib ,多试几次
~ » pip install matplotlib kongfanyu@kongfanyudeMacBook-Pro
Collecting matplotlib
Using cached matplotlib-3.6.2-cp310-cp310-macosx_10_12_x86_64.whl (7.3 MB)
Collecting packaging>=20.0
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x110a55540>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/8f/7b/42582927d281d7cb035609cd3a543ffac89b74f3f4ee8e1c50914bcb57eb/packaging-22.0-py3-none-any.whl
Downloading packaging-22.0-py3-none-any.whl (42 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.6/42.6 kB 29.2 kB/s eta 0:00:00
Collecting cycler>=0.10
Downloading cycler-0.11.0-py3-none-any.whl (6.4 kB)
Requirement already satisfied: numpy>=1.19 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (1.24.1)
Collecting python-dateutil>=2.7
Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting contourpy>=1.0.1
Using cached contourpy-1.0.6-cp310-cp310-macosx_10_9_x86_64.whl (240 kB)
Requirement already satisfied: pillow>=6.2.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (9.3.0)
Collecting kiwisolver>=1.0.1
Downloading kiwisolver-1.4.4-cp310-cp310-macosx_10_9_x86_64.whl (65 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.5/65.5 kB 7.8 kB/s eta 0:00:00
Collecting fonttools>=4.22.0
Using cached fonttools-4.38.0-py3-none-any.whl (965 kB)
Collecting pyparsing>=2.2.1
Using cached pyparsing-3.0.9-py3-none-any.whl (98 kB)
Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
Installing collected packages: python-dateutil, pyparsing, packaging, kiwisolver, fonttools, cycler, contourpy, matplotlib
Successfully installed contourpy-1.0.6 cycler-0.11.0 fonttools-4.38.0 kiwisolver-1.4.4 matplotlib-3.6.2 packaging-22.0 pyparsing-3.0.9 python-dateutil-2.8.2
------------------------------------------------------------
~ » pip list kongfanyu@kongfanyudeMacBook-
Package Version
------------------ ---------
beautifulsoup4 4.10.0
certifi 2021.10.8
charset-normalizer 2.0.6
contourpy 1.0.6
cycler 0.11.0
fonttools 4.38.0
idna 3.2
kiwisolver 1.4.4
matplotlib 3.6.2
numpy 1.24.1
packaging 22.0
Pillow 9.3.0
pip 22.3.1
pyparsing 3.0.9
python-dateutil 2.8.2
pywifi 1.1.12
requests 2.26.0
setuptools 57.4.0
six 1.16.0
soupsieve 2.2.1
urllib3 1.26.7
w3lib 1.22.0
wheel 0.37.0
wordcloud 1.8.2.2
修改项目的解释器,不要用虚拟路径
案例第一步,结巴分词:
# -*- coding = utf-8 -*-
# @Time: 2024/1/2 15:31
# @File: wordCloudDemo.py
# @Software: PyCharm
import jieba
import sqlite3 # 数据库
con = sqlite3.connect("movie.db")
cur = con.cursor()
sql = "select instroduction from movie250"
data = cur.execute(sql)
text = ""
for item in data:
text = text + item[0]
print(text)
cur.close()
con.close()
# 用结巴分词
cut = jieba.cut(text)
string = ' '.join(cut)
print(len(string))
执行效果:
Building prefix dict from the default dictionary ...
Loading model from cache /var/folders/lz/ktdhrh1s7w3ggytjy5046dxh0000gn/T/jieba.cache
Loading model cost 0.659 seconds.
Prefix dict has been built successfully.
5425
进程已结束,退出代码0
完成的代码:
# -*- coding = utf-8 -*-
# @Time: 2024/1/2 15:31
# @File: wordCloudDemo.py
# @Software: PyCharm
import jieba # 分词
import sqlite3 # 数据库
from wordcloud import WordCloud
from PIL import Image # 图片处理 # 词云
import numpy as np # 矩阵运算
from matplotlib import pyplot as plt # 绘图,数据可视化
con = sqlite3.connect("movie.db")
cur = con.cursor()
sql = "select instroduction from movie250"
data = cur.execute(sql)
text = ""
for item in data:
text = text + item[0]
# print(text)
cur.close()
con.close()
# 分词
cut = jieba.cut(text)
string = ' '.join(cut)
# print("分词后的数据: \n" + string)
print(len(string)) # 5425
img = Image.open(r'./static/assets/img/tree.jpg')
img_array = np.array(img) # 讲图片转换为数组
wc = WordCloud(background_color='white',
mask=img_array,
font_path="方正小标宋.ttf")
wc.generate_from_text(string)
# 绘制图片
fig = plt.figure(1)
plt.imshow(wc) # 显示
plt.axis('off') # 是否显示坐标轴
# plt.show() # 显示
plt.savefig(r'./static/assets/img/word.jpg', dpi=500) # 输出词云图片到文件夹
- 点赞
- 收藏
- 关注作者
评论(0)