Python制作词云图

利用python制作词云图 词云图是一种为了文本数据的视觉表示,由词汇组成类似云的彩色图形,当需要统计文本中出现词汇的规模大小时,我们可以使用此类图形。本文将介绍如何用python代码制作词云图。 阅读详情

安装相关的第三方包

pip3 install numpy matplotlib pillow wordcloud -i https://pypi.douban.com/simple

制作简单的词云

article.txt的内容如下

Python is developed by Guido van Rossum. Guido van Rossum started implementing Python in 1989. Python is a very simple programming language so even if you are new to programming,
you can learn python without facing any issues.Readable: Python is a very readable language.
Easy to Learn: Learning python is easy as this is a expressive and high level programming language, which means it is easy to understand the language and thus easy to learn.
Cross platform: Python is available and can run on various operating systems such as Mac, Windows, Linux, Unix etc. This makes it a cross platform and portable language.
Open Source: Python is a open source programming language.
Large standard library: Python comes with a large standard library that has some handy codes and functions which we can use while writing code in Python.
Free: Python is free to download and use. This means you can download it for free and use it in your application. See: Open Source Python License. Python is an example of a FLOSS (Free/Libre Open Source Software), which means you can freely distribute copies of this software, read its source code and modify it.
Supports exception handling: If you are new, you may wonder what is an exception? An exception is an event that can occur during program exception and can disrupt the normal flow of program. Python supports exception handling which means we can write less error prone code and can test various scenarios that can cause an exception later on.
Advanced features: Supports generators and list comprehensions. We will cover these features later.
Automatic memory management: Python supports automatic memory management which means the memory is cleared and freed automatically. You do not have to bother clearing the memory.

实现代码如下

from wordcloud import WordCloud

# 读取文本
text = open('article.txt', 'r', encoding='utf-8').read()

# 生成词云
wc = WordCloud(background_color='white', width=600, height=400,
               max_font_size=80).generate(text)
# 保存词云图片
wc.to_file('ciyun.png')

效果图如下
在这里插入图片描述

制作基于图像颜色的词云

superman.txt的内容如下

A superhero who needs no introduction, it's still worth noting that Superman isn't just a comic book icon, he's the comic book icon. Debuting in the wake of the Great Depression and just before World War II, Superman set the stage for the DC Universe and all superhero comics to follow.
Below you'll find essential statistics and biographical information about Superman, as well as some of his major comic book appearances.
Superman’s origin has been one of much change over the last many decades. His origin has been changed many times to adjust for the changes in our own culture and to bring in other story elements from other comics. There have even been many different parallel Supermen that exist in alternate realities. While the most current origin of Superman is often thrown into a state of flux with DC Universe events like the 2006 series, “Infinite Crisis,” or the 1986 series, “Crisis on Infinite Earths,” the core tenets of his origins have remained the same.
Superman is the last of a dying race from the planet Krypton. His Krypton name is Kal-El. His father, Jor-El was a great scientist and saw the warning signs that their planet was doomed to destruction. A council heard his discoveries, but dismissed them and forbid Jor-El to speak of this to anyone. Realizing that his family was in danger, Jor-El started to construct a rocket that would take him, his son and wife Lara away from Krypton, but it was too late.
Jor-El had only constructed a small model of the rocket when disaster struck, Lara decided to stay behind with Jor-El to give their baby a better chance of survival. Lara and Jor-El put their baby into the rocket and directed it to Earth, where it landed and was discovered by John and Martha Kent, near the town of Smallville.
As young Kal-El grew up, he discovered his amazing powers of speed, strength, and invulnerability and eventually flight. It would be in Smallville with the Kents that the newly named Clark learned many of his life lessons and became the honest and good man that many know him to be today. After he graduated, he went to Metropolis University and majored in journalism, eventually getting a job with The Daily Planet as a reporter.
It would be at The Daily Planet that Clark would first don the Superman costume and save Metropolis time and again. He also met Lois Lane, a fellow reporter, and became romantically involved with her. Lois Lane later became Superwoman a few different times in the comic books.
One of Superman’s darkest times was when he faced the nigh-unstoppable villain Doomsday, in DC's "The Death of Superman." The battle lasted for days, but when the dust settled, both hero and villain were slain. Superman was dead. This comic book storyline influenced the 2016 movie "Batman v Superman: Dawn of Justice".
The backlash from his death resulted in four separate beings taking up the Superman mantle. There was a cyborg, a new Superboy, Steel, and an alien being with the memories of Superman. It would later come out that Superman was not dead, and resurfaced without his powers. He eventually gained them back and was reunited with Lois, whom he later married.
Superman has continued to fight evil and protect Earth from all challengers. Despite his many continuity changes, Superman is still as powerful and noble as ever. He is a modern day hero with over eighty years of continuity behind him. To many, though, he will always be that lovable boy from Smallville who became a mighty man of steel.
A superhero who needs no introduction, it's still worth noting that Superman isn't just a comic book icon, he's the comic book icon. Debuting in the wake of the Great Depression and just before World War II, Superman set the stage for the DC Universe and all superhero comics to follow.
Below you'll find essential statistics and biographical information about Superman, as well as some of his major comic book appearances.
Superman’s powers have changed greatly over the years. In the first incarnation of "Superman" by Siegel and Shuster, Superman had super strength, being able to lift a car over his head. He also had the ability to run extremely fast and to jump as much as an eighth of a mile into the air. Later writers have increased Superman’s powers, taken them away, re-raised them to near omnipotence and then back again.
The current incarnation of Superman sees him near his omnipotent (near god-like) powers. Superman has the power of flight, being able to fly into space and survive in a vacuum. His strength has also been increased, allowing him to lift entire mountains. He has a heat vision that allows him to shoot laser-like beams. He also has x-ray and telescopic vision. Superman’s breath is so powerful that he can knock over vehicles and even freeze objects.
The origin of Superman’s powers has also been something that has been transformed over the years. The basic tenant is still there, that Superman came from Krypton to Earth to survive a calamity. At first, there was no mention of how Superman got his powers. Later it was decided that Kryptonians live under a red star and when they are exposed to light from a yellow star, their powers emerge.

原图如下
在这里插入图片描述
实现代码如下

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

text = open('superman.txt', encoding='utf-8').read()  # 读取文本

img_coloring = np.array(Image.open("superman.png"))
stopwords = set(STOPWORDS)  # 停用词(需要过滤掉)
stopwords.add("said")

wc = WordCloud(background_color="white", max_words=3000, mask=img_coloring,
               stopwords=stopwords, max_font_size=40, min_font_size=5, random_state=42)
# 生成词云
wc.generate(text)
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:711312441
# 获取图片颜色
image_colors = ImageColorGenerator(img_coloring)

fig, axes = plt.subplots(1, 3)  # 创建一行三列的画布
axes[0].imshow(wc, interpolation="bilinear")  # 普通词云
axes[1].imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")  # 加上主色调的词云
axes[2].imshow(img_coloring, cmap=plt.cm.gray, interpolation="bilinear")  # 原图
for ax in axes:
    ax.set_axis_off()

plt.savefig('superman_wc.png', dpi=500)  # 保存图片
plt.show()  # 展示

效果图如下
在这里插入图片描述

python词云制作(最全最详细的教程) 今天手把手教大家用python制作词云 首先呢,需要准备的操作是: 1、安装好python3系列版本 2、安装python第三方库wordcloud;(安装教程以及可能会遇到的各种错误,我整理了一篇文章) 阅读详情

相关推荐

利用python制作词云图,分词,提取关键词

利用python制作词云图保姆及教程 前言 一、环境配置 1.要有python的运行环境 2.需要导入jieba ,wordcloud等模块 pip install jieba pip install wordcloud 注意:有时候导入模块可能会出错,我们可以换一个镜像网站进行安装 pip install 安装包名字 -i http://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com 3.准备一个用于分析的文章(此处使用(边城.t

qq_50791662的博客 6046

python绘制词云图

本文讲解如何通过python绘制词云图

jakelihua 1万+

python绘制词云图的心得体会,python如何制作词云图

大家好,小编来为大家解答以下问题,python制作词云图片的三个步骤,python绘制词云图的心得体会,今天让我们一起来看看吧!**

阿发狗伪原创 579

使用Python绘制各种方法的词云图

在数据可视化图表中,词云图的应用随处可见。它通常是对输入的一段文字进行词频提取,然后以根据词汇出现频率的大小集中显示高频词,简洁直观高效,今天小编就来分享一下在Python如何绘制出来精湛的词云图。 我们先来尝试绘制一张简单的词云图,用到的Python当中的wordcloud模块来绘制,

zz3897642的博客 1万+

python制作京东评论词云图

爬虫的小试牛刀

weixin_42957905的博客 1844

【3DSlicer】基于心脏ct影像重建3d模型及导出

软件:3DSlicer4.11 基于心脏ct影像重建3d模型

Liuees的博客 1万+

python制作词云图

一、相关库及函数 pip install wordcloud 文档:wordcloud.WordCloud — wordcloud 1.8.1 documentation 1、生成词云对象 classwordcloud.WordCloud(font_path=None,width=400,height=200,margin=2,ranks_only=None,prefer_horizontal=0.9,mask=None,scale=1,color_func=None...

weixin_42125148的博客 4836

Sparse4Dv3 代码复现

打包数据集的元信息和标签,并生成所需的.pkl文件。最后还输出了很多跟踪相关的东西...然后通过K-means生成锚点。编译可变形聚合CUDA算子。安装motmetrics。

m0_60857098的博客 4490

Python 制作词云图

词云图是一种将文本数据中的关键词以不同大小、颜色和字体显示在图形中的可视化技术。关键词的字体大小通常与其在文本中出现的频率成正比,从而突出显示文本中的重要信息。

专注于全栈开发领域 1982

别再只看成交量了!用通达信这个VOL监测公式,一眼识别主力吸筹与出货信号

本文详细介绍了通达信VOL监测公式的实战应用,通过彩色量柱和换手率分层技术,帮助投资者识别主力吸筹与出货信号。该公式结合动态量能标准化和多周期均线组合,提供可视化主力行为图谱,提升交易决策的准确性。

weixin_28693447的博客 664

第1章:Zephyr 开发环境搭建与 Hello World

📝【摘要】本文是Zephyr RTOS开发入门指南的第一章,详细介绍了从环境搭建到首个应用运行的完整流程。主要内容包括:1️⃣ Zephyr项目结构解析与west工具链部署;2️⃣ 开发环境配置(Linux/WSL)及SDK安装;3️⃣ 两种实践路径——官方HelloWorld样例与自定义应用开发;4️⃣ 实际硬件部署方法(nRF/STM32)及常用west命令速查;5️⃣ 典型问题解决方案。通过qemu_x86模拟器演示了基础打印功能实现,为后续深度开发奠定基础。

hemoparrot的专栏 1361

python爬取新闻,制作词云图

python词云图

m0_73222051的博客 4998

nCode:GlyphWorks案例教程一

nCode GlyphWorks案例一Comparing Frequency Content of Two Different Tests案例文件目标配置选择数据 Comparing Frequency Content of Two Different Tests 在本案例中,我们将比较来自两个不同测试的六个加速计通道的频率内容。将三轴加速度计放置在车辆的左前角和右前角,然后分别在两个不同的试验路面上行驶12分钟以上。 案例文件 此案例的文件(采用nCode DAC格式)位于nCode安装目录下的\demo

追月行的博客 1万+

数据可视化之利用Python制作词云图

制作词云图一.词云图介绍二.wordcloud方法二. stylecloud方法 一.词云图介绍 词云图可以看作是文本数据的视觉表示,由词汇组成类似云的彩色图形。相对其它诸多用来显示数值数据的图表,词云图的独特之处在于,可以展示大量文本数据。文本数据中每个词的重要性以字体大小或颜色显示,因此词云可以很好地表明每个单词在特定文本体中被提及的频率(即词频分布),通过使用不同的颜色和大小来表示不同级别的...

小笨聪 1万+

华为OD机试双机位C卷-用户入网定期复评(C/C++/Py/Java/Js/Go)

华为OD机试(机考)双机位C卷 【用户入网定期复评】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【用户入网定期复评】。

qq_45776114的博客 841

2023华为OD机试真题B卷 Python 实现【最长的元音子串长度】

定义当一个字符串只有元音字母(a,e,i,o,u,A,E,I,O,U)组成,称为元音字符串,现给定一个字符串,请找出其中最长的元音字符串,并返回其长度,如果找不到请返回0, 字符串中任意一个连续字符组成的子序列称为该字符串的子串

MISAYAONE的博客 903

python词云图的制作_Python实战—词云图制作

东边日出西边雨,道是无晴却有晴。今天介绍一款把妹神器,用Python做词云图,绝对惊艳! 不要感谢我,我是丘比特的接班人。老司机们,请上车...... 下面介绍,如何用Python做出好看的词云图。随我一起边欣赏、边实践 一、导入标准库conda install jiebaconda install wordcloud #安装生成词云的依赖包和运行环境conda install PI...

weixin_39758956的博客 959

Dify框架面试内容整理-如何进行Dify的二次开发?

无论是轻量修改界面,还是深度集成自定义模型、Agent、知识库,你都可以很方便地进行二次开发。,你可以基于它的开源架构对功能进行扩展、集成第三方服务、修改界面风格,甚至定制新的AI工作流程。前端:Node.js >= 16,React + TypeScript + Tailwind。接入自己的模型API(如:调用LLaMA、Baichuan、ChatGLM)。修改路径:/api/app/services/providers/可接入你自己的向量数据库(Qdrant、Milvus、FAISS)

不务正业的猿 2009

利用python制作漂亮的词云图

作者:黎智煊 叩丁狼高级讲师。原创文章,转载请注明出处。前言:现在制作一个词云图还是简单的事,例如这个-> https://wordart.com (选择Noto Sans S Chinese Regular字体就可以制作中文词云),然后选择”SHAPES”可以导入自己想制作词云形状图片,记得这个图片不要太复杂,颜色比较单一.but,图样图森破,不是各种限制就是收费了,真的是调理农 所以这里...

wolfcode_cn的博客 3万+

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【python】词云图制作

优美词云图制作

CBCY_csdn的博客 1万+

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【Python成长之路】词云图制作

【写在前面】 以前看到过一些大神制作的词云图,觉得效果很有意思。如果有朋友不了解词云图的效果,可以看下面的几张图(图片都是网上找到的); 网上找了找相关的软件,有些软件制作 还要付费。结果前几天在大神的公众号里的文章中看到了python也能实现,而且效果也很不错 。那还等什么,不赶紧盘它? 【示例代码】 # coding=utf-8 # @Auther : "鹏哥贼...

华为云官方博客 1859

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

python 词云图_【Python成长之路】词云图制作

【写在前面】以前看到过一些大神制作的词云图 ,觉得效果很有意思。如果有朋友不了解词云图的效果,可以看下面的几张图(图片都是网上找到的);网上找了找相关的软件,有些软件制作 还要付费。结果前几天在大神的公众号里的文章中看到了python也能实现,而且效果也很不错 。那还等什么,不赶紧盘它?【示例代码】# coding=utf-8 # @Auther : "鹏哥贼优秀" # @Date : 2019/...

weixin_39631899的博客 233

python词云图制作壮观天体照_【Python成长之路】词云图制作

【写在前面】以前看到过一些大神制作的词云图,觉得效果很有意思。如果有朋友不了解词云图的效果,可以看下面的几张图(图片都是网上找到的);网上找了找相关的软件,有些软件制作 还要付费。结果前几天在大神的公众号里的文章中看到了python也能实现,而且效果也很不错 。那还等什么,不赶紧盘它?【示例代码】# coding=utf-8# @Auther : "鹏哥贼优秀"# @Date : 2019/7/...

weixin_39740737的博客 140

词云图python_用python来制作词云图

前言词云图顾名思义就是把文本中的单词制作成图片的形式,其中单词出现的频率越高,那么相应的在图片中所占的面积就大,如何制作词云图呢,首先获取文本的内容,通过分词解析单词出现的频率,然后生成对应的词云图pyecharts环境以及相关模块的安装词云图的绘制关于词云图的一些看法pyecharts环境的安装作者的python版本是2.7,由于pyecharts的1.0版本以上不再支持python2.7,所以...

weixin_39636176的博客 304
上一篇: Python中print和pprint两者的区别
下一篇: Python十道基础编程题
Python热爱者
博客等级 码龄7年 3059粉丝 1330原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值