Python 实例5 生成词云图

Python生成词云图:这里引用《勇敢的心》经典台词,进行词云图生成展示。

db1ccf7b4a3b0223bdae16f8c44647af.png

from wordcloud import WordCloud


# 文本地址
text_path = 'test.txt'
# 示例文本
scr_text = "Fight,and you may die." \
           "Run,and you'll live at least a while." \
           "And dying in your beds many years from now." \
           "Would you be willing to trade?" \
           "All the days from this day to that, " \
           "for one chance,just one chance, " \
           "to come back here and tell our enemies that they may take our lives, " \
           "but they'll never take our Freedom!Freedom——"


# 保存示例文本
with open(text_path, 'w', encoding='utf-8') as f:
    f.write(scr_text)


# 读取文本
with open(text_path, 'r', encoding='utf-8') as f:
    # 这里text是一个字符串
    text = f.read()
# 生成词云, WordCloud对输入的文本text进行切词展示。
wordcloud = WordCloud().generate(text)


import matplotlib.pyplot as plt


plt.axis("off")
plt.imshow(wordcloud, interpolation='bilinear')
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值