使用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')

效果图如下

image-ciyun

制作基于图像颜色的词云

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)

# 获取图片颜色
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()  # 展示

效果图如下

image-superman_wc

posted @ 2021-04-16 16:06  蓝莓薄荷  阅读(780)  评论(0编辑  收藏  举报