教你使用Python绘制词云图【附代码】

在这里插入图片描述
代码如下:

import matplotlib.pyplot as plt
from wordcloud import WordCloud

# 方面列表和它们的权重
aspects = {
    "Hello World": 3,
    "Variables and Data Types": 4,
    "Lists": 4,
    "Dictionaries": 4,
    "Loops": 4,
    "Conditional Statements": 4,
    "Functions": 4,
    "Module Import": 4,
    "Exception Handling": 4,
    "Date and Time": 5,
    "Random Number Generation": 5,
    "Regular Expressions": 5,
    "CSV File Handling": 5,
    "JSON Handling": 5,
    "BeautifulSoup": 5,
    "File Operations": 5,
    "Multithreading": 5,
    "Tkinter": 5,
    "Pandas": 6,
    "asyncio": 5,
    "XGBoost": 6,
    "Matplotlib": 5,
    "Scikit-Learn": 5,
    "Selenium": 5,
    "Flask": 1,
    "Web Requests": 3,
    "SQLite": 3,
    "Pillow": 3,
    "Numpy": 6,
    "Faker": 3,
    "cryptography": 3,
    "Socket Programming": 3,
    "threading": 3,
    "re": 4,
    "NLTK": 5,
    "Keras": 7,
    "OpenCV": 7,
    "Scrapy": 7,
    "FastAPI": 3,
    "SQLAlchemy": 5,
    "Seaborn": 5,
    "Plotly": 5,
    "argparse": 5,
    "Flask-RESTful": 3,
    "BeautifulSoup": 3,
    "spaCy": 6,
    "hashlib": 5,
    "Pickle": 5,
    "concurrent.futures": 5,
    "Scrapy": 6
}

# 将方面列表和权重转化为文本
aspects_text = " ".join([aspect for aspect, weight in aspects.items() for _ in range(weight)])

# 创建WordCloud对象
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(aspects_text)

# 显示云图
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

注意事项:

  1. 需要先下载包
  2. 如果报错ValueError: Only supported for TrueType fonts —> 均是pillow包版本问题
    直接终端输入指令:
pip install pillow==9.3.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

pillow包的版本问题!直接改成9.3.0的版本即可!建议使用清华镜像源安装

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗半里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值