用Python生成“数据可视化云图”(WordCloud using Python)

Github下载链接:https://github.com/nikhilkumarsingh/wordcloud-example
youtube视频链接:https://www.youtube.com/watch?v=95p3cVkqYHQ

What is a wordcloud?

什么是文字云图?

An image composed of words used in a particular text or subject, in which the size of each word indicates its frequency or importance.

由在特定文本或主题中使用的单词组成的图像,其中每个单词的大小表示其频率或重要性。

Installation

安装

Install wordcloud using a simple pip command.
使用简单的pip命令安装wordcloud。

pip install wordcloud

用Python生成“数据可视化云图”(WordCloud using Python)
wikipedia library is used for extracting wikipedia articles on any given topic. Install it using this pip command:

*库用于提取有关任何给定主题的*文章。使用以下pip命令安装它:

pip install wikipedia

用Python生成“数据可视化云图”(WordCloud using Python)
Usage
Run python script as:
用法
以如下方式运行python脚本:

python mywc.py <query>

For example:
例如:

python mywc.py China

will create wordcloud for the topic ‘china’ which looks like this:

将为主题“China”创建wordcloud,如下所示:
用Python生成“数据可视化云图”(WordCloud using Python)

program:

程序:

import sys
from os import path
import numpy as np
from PIL import Image
import wikipedia
from wordcloud import WordCloud, STOPWORDS

# get path to script's directory
currdir = path.dirname(__file__)

def get_wiki(query):
	# get best matching title for given query
	title = wikipedia.search(query)[0]

	# get wikipedia page for selected title
	page = wikipedia.page(title)
	return page.content


def create_wordcloud(text):
	# create numpy araay for wordcloud mask image
	mask = np.array(Image.open(path.join(currdir, "cloud.png")))

	# create set of stopwords	
	stopwords = set(STOPWORDS)

	# create wordcloud object
	wc = WordCloud(background_color="white",
					max_words=200, 
					mask=mask,
	               	stopwords=stopwords)
	
	# generate wordcloud
	wc.generate(text)

	# save wordcloud
	wc.to_file(path.join(currdir, "wc.png"))


if __name__ == "__main__":
	# get query
	query = sys.argv[1]

	# get text for given query
	text = get_wiki(query)
	
	# generate wordcloud
	create_wordcloud(text)

云图素材

用Python生成“数据可视化云图”(WordCloud using Python)

用Python生成“数据可视化云图”(WordCloud using Python)用Python生成“数据可视化云图”(WordCloud using Python) Lukey Alvin 发布了44 篇原创文章 · 获赞 83 · 访问量 1万+ 私信 关注
上一篇:浮点数计算误差


下一篇:grep过滤日志