当前wordcloud版本:1.81
wordcloud.py模块结构
wordcloud.py模块是wordcloud包主要模块。
wordcloud.py模块结构如下,包括:
变量:FILE、FONT_PATH、STOPWORDS
类:WordCloud、IntegralOccupancyMap、colormap_color_func
函数: random_color_func和 get_single_color_func
其中WordCloud类、 STOPWORDS变量、 random_color_func函数、 get_single_color_func函数暴露到了wordcloud包的命名空间。

FILE:获取模块的物理路径。FONT_PATH:获取默认字体的物理路径。STOPWORDS:将默认的停用词表转换为集合对象(set)。WordCloud:生成词云图的主接口。random_color_func:默认颜色生成方式,根据HSL色彩模式随机生成颜色,其中hue为固定值80%,lumination为固定值50%。get_single_color_func:返回一个返回值为RGB颜色的函数,颜色转换依靠PIL.ImageColor类和内置库colorsys。colormap_color_func:通过matplotlib颜色映射表生成一个返回值为PIL(Pillow)`支持的RGB颜色字符串的函数。IntegralOccupancyMap:根据文本词频和背景图构造积分占据栅格地图,对图像进行布局。
random_color_func解读
random_color_func功能为根据HSL色彩模式随机生成颜色,其中hue为固定值80%,lumination为固定值50%。
函数的签名为:random_color_func(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None)
参数word, font_size, position, orientation都将被忽略。
random_state用于生成随机数字。取值为random.Random对象或None,默认值为None,直接使用random.Random()
案例:演示random_color_func的功能
from wordcloud import random_color_func
# 调用函数随机生成颜色,返回值为PIL(Pillow)支持的hsl颜色字符串
color=random_color_func()
print(color)
输出为:hsl(206, 80%, 50%)
源码
def random_color_func(word=None, font_size=None, position=None,
orientation=None, font_path=None, random_state=None):
if random_state is None:
random_state = Random()
return "hsl(%d, 80%%, 50%%)" % random_state.randint(0, 255)
colormap_color_func解读
colormap_color_func类的功能为通过matplotlib颜色映射表生成一个返回值为PIL(Pillow)支持的RGB颜色的函数。
colormap_color_func类的签名为class colormap_color_func(colormap)
该类只定义了两个类,构造方法__init__和调用方法__call__。
构造方法__init__
构造方法签名为def __init__(self, colormap)。
参数为 colormap:matplotlib色彩映射表的名称。字符串。必备参数。
根据源码self.colormap = plt.cm.get_cmap(colormap)可知,colormap为matplotlib.colors.Colormap实例。
调用方法__call__
调用方法签名为def __call__(self, word, font_size, position, orientation, random_state=None, **kwargs)。
参数word, font_size, position, orientation都将被忽略。
random_state用于生成随机数字。取值为random.Random对象或None,默认值为None,直接使用random.Random()
案例:演示colormap_color_func类的功能
import numpy as np
from PIL import ImageColor
import matplotlib.pyplot as plt
from wordcloud.wordcloud import colormap_color_func
# 根据colormap构造函数
color_func=colormap_color_func("autumn")
# 调用函数随机生成颜色,返回值为PIL(Pillow)支持的RGB字符串
color=color_func(word=None,font_size=None,font_path=None,position=None,orientation=None)
print(color)
# 将字符串转换为RGB三元组
color_rgb=ImageColor.getrgb(color)
# 显示最终颜色
plt.subplot(facecolor=np.array(color_rgb)/255)
输出为颜色为autumn颜色映射表中的一个随机颜色。

源码
class colormap_color_func(object):
def __init__(self, colormap):
import matplotlib.pyplot as plt
self.colormap = plt.cm.get_cmap(colormap)
def __call__(self, word, font_size, position, orientation,
random_state=None, **kwargs):
if random_state is None:
random_state = Random()
r, g, b, _ = np.maximum(0, 255 * np.array(self.colormap(
random_state.uniform(0, 1))))
return "rgb({:.0f}, {:.0f}, {:.0f})".format(r, g, b)
get_single_color_func解读
get_single_color_func的功能为返回一个返回值为RGB颜色的函数。
get_single_color_func函数的签名为def get_single_color_func(color)。
参数为color取值为PIL(Pillow)支持的颜色字符串。
返回值为一个返回值为PIL(Pillow)支持的RGB颜色字符串。
颜色转换依靠PIL.ImageColor类和内置库colorsys。转换过程如下:
先通过PIL.ImageColor类将color参数转换为RGB模式颜色值。
然后再通过colorsys模块将其转换为HSV模式颜色值。
再构造single_color_func函数(功能类似于random_color_func),该函数将HSV模式颜色值转换为RGB模式颜色值,其中H和S分量保持不变,V分量(明度)取值为[0.2-1]之间的随机值。
最后返回single_color_func函数。
案例:演示get_single_color_func功能
from PIL import ImageColor
import matplotlib.pyplot as plt
from wordcloud import get_single_color_func
# 调用函数生成颜色
color=get_single_color_func('#ff0000')
# 输出随机生成的颜色
print(color())
# 将字符串转换为RGB三元组
color_rgb=ImageColor.getrgb(color())
# 显示对比输入颜色和最终颜色
plt.subplot(211,facecolor='#ff0000')
plt.subplot(212,facecolor=np.array(color_rgb)/255)
输出为:
rgb(107, 0, 0)

源码
def get_single_color_func(color):
old_r, old_g, old_b = ImageColor.getrgb(color)
rgb_max = 255.
h, s, v = colorsys.rgb_to_hsv(old_r / rgb_max, old_g / rgb_max,
old_b / rgb_max)
def single_color_func(word=None, font_size=None, position=None,
orientation=None, font_path=None, random_state=None):
if random_state is None:
random_state = Random()
r, g, b = colorsys.hsv_to_rgb(h, s, random_state.uniform(0.2, 1))
return 'rgb({:.0f}, {:.0f}, {:.0f})'.format(r * rgb_max, g * rgb_max,
b * rgb_max)
return single_color_func
342



被折叠的 条评论
为什么被折叠?



