wordcloud + lyrics生成歌词词云
根据图像生成歌词词云
class wordcloud.WordCloud(font_path=None, width=400, height=200, margin=2, ranks_only=None, prefer_horizontal=0.9,mask=None, scale=1, color_func=None, max_words=200, min_font_size=4, stopwords=None, random_state=None,background_color='black', max_font_size=None, font_step=1, mode='RGB', relative_scaling=0.5, regexp=None, collocations=True,colormap=None, normalize_plurals=True)
参数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 取代。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
//设置需要屏蔽的词,如果为空,则使用内置的STOPWORDSbackground_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_funcregexp : string or None (optional)
//使用正则表达式分隔输入的文本collocations : bool, default=True
//是否包括两个词的搭配colormap : string or matplotlib colormap, default=”viridis”
//给每个单词随机分配颜色,若指定color_func,则忽略该方法。
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# lyrics
text = open("lyrics.txt",'r', encoding='UTF-8').read()
# background
bg_pic = plt.imread("idol_huge.jpg")
# word
wordcloud = WordCloud(
font_path="xingkai.TTF",
mask=bg_pic,
scale=2,
background_color="white",
max_words=300,
max_font_size=50).generate(text)
wordcloud.to_file('lyrics_wordcloud.jpg')
偶像老胡
歌词词云
具体实现参考 github/dreamhomes