wordcloud tutorial

installation 

1. Use pip install wordc directly, loud may report various errors

 2. Download the library file wordcloud‑1.3.2‑cp36‑cp36m‑win32.whl, and then pip install wordcloud‑1.3.2‑cp36‑cp36m‑win32.whl 

pyhton library file download address: https://www.lfd.uci.edu/~gohlke/pythonlibs/

Case

  
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS
import jieba
import numpy as np
from PIL import Image
def wordyun():
 #导入背景图   
backgrim=np.array(Image.open("C:\\Users\SAMSUNG\PycharmProjects\practice0829\qqzon\\bg.jpg"))
 #导入文本文件   text=open("C:\\Users\SAMSUNG\PycharmProjects\practice0829\qqzon\\1154540719worldcloud.txt",encoding='utf-8').read()
    #jieba分词
    wordlist = jieba.cut(text, cut_all=True)
    wl = " ".join(wordlist)
#设置参数
    wordcloud=WordCloud(
        background_color='white',  #背景颜色
        mask=backgrim ,   #背景图片
        max_words = 300,  # 设置最多现实的词数
        stopwords=STOPWORDS,  # 设置停用词
        max_font_size=200,    # 设置字体最大值
        font_path='C:/Users/Windows/fonts/STXINGKA.TTF',#设置字体,路径在电脑内
        width=1600,
        height=1000,
        random_state=30,  # 设置有多少种随机生成状态,即有多少种配色方案
        # scale=.5
    ).generate(text)
#改变字体颜色
    image_colors = ImageColorGenerator(backgrim)
#展示词云
    plt.imshow(wordcloud)
#是否显示想x,y坐标
    plt.axis("off")
    plt.show()
#写入文件
    wordcloud.to_file('py_book1.png')  # 把词云保存下
wordyun()

Word cloud parameters:

font_path : string //字体路径,需要展现什么字体就把该字体路径+后缀名写上,如:font_path = '黑体.ttf'
 
width : int (default=400) //输出的画布宽度,默认为400像素
 
height : int (default=200) //输出的画布高度,默认为200像素
 
prefer_horizontal : float (default=0.90) //词语水平方向排版出现的频率,默认 0.9 (所以词语垂直方向排版出现频率为 0.1 )
 
mask : nd-array or None (default=None) //如果参数为空,则使用二维遮罩绘制词云。如果 mask 非空,设置的宽高值将被忽略,遮罩形状被 mask 取代。除全白(#FFFFFF)的部分将不会绘制,其余部分会用于绘制词云。如:bg_pic = imread('读取一张图片.png'),背景图片的画布一定要设置为白色(#FFFFFF),然后显示的形状为不是白色的其他颜色。可以用ps工具将自己要显示的形状复制到一个纯白色的画布上再保存,就ok了。
 
scale : float (default=1) //按照比例进行放大画布,如设置为1.5,则长和宽都是原来画布的1.5倍。
 
min_font_size : int (default=4) //显示的最小的字体大小
 
font_step : int (default=1) //字体步长,如果步长大于1,会加快运算但是可能导致结果出现较大的误差。
 
max_words : number (default=200) //要显示的词的最大个数
 
stopwords : set of strings or None //设置需要屏蔽的词,如果为空,则使用内置的STOPWORDS
 
background_color : color value (default=”black”) //背景颜色,如background_color='white',背景颜色为白色。
 
max_font_size : int or None (default=None) //显示的最大的字体大小
 
mode : string (default=”RGB”) //当参数为“RGBA”并且background_color不为空时,背景为透明。
 
relative_scaling : float (default=.5) //词频和字体大小的关联性
 
color_func : callable, default=None //生成新颜色的函数,如果为空,则使用 self.color_func
 
regexp : string or None (optional) //使用正则表达式分隔输入的文本
 
collocations : bool, default=True //是否包括两个词的搭配
 
colormap : string or matplotlib colormap, default=”viridis” //给每个单词随机分配颜色,若指定color_func,则忽略该方法。
 
 
 
fit_words(frequencies)  //根据词频生成词云
generate(text)  //根据文本生成词云
generate_from_frequencies(frequencies[, ...])   //根据词频生成词云
generate_from_text(text)    //根据文本生成词云
process_text(text)  //将长文本分词并去除屏蔽词(此处指英语,中文分词还是需要自己用别的库先行实现,使用上面的 fit_words(frequencies) )
recolor([random_state, color_func, colormap])   //对现有输出重新着色。重新上色会比重新生成整个词云快很多。
to_array()  //转化为 numpy array
to_file(filename)   //输出到文件

 

numpy.array 

Python provides a list container, which can be used as an array. But the elements in the list can be any object, so the pointer to the object is stored in the list, so in order to save a simple list [1,2,3]. You need three pointers and three integer objects. For numerical operations, this structure is obviously not efficient enough.
    Although Python also provides the array module, it only supports one-dimensional arrays, does not support multi-dimensional arrays (it is biased towards matrix understanding in TensorFlow), and does not have various operation functions. Therefore, it is not suitable for numerical operations.
    The emergence of NumPy makes up for these shortcomings.

(——Excerpt from "Python Scientific Computing" by Zhang Ruoyu)
Achievement Show

 

 

Guess you like

Origin blog.csdn.net/skylibiao/article/details/89702746