wordcloud
使用文本数据创建文字云图

语法
说明
wordcloud(___, 使用一个或多个名称-值对组参量指定其他 Name,Value)WordCloudChart 属性。
wordcloud( 在由 parent,___)parent 指定的图窗、面板或选项卡上创建文字云。
返回 wc = wordcloud(___)WordCloudChart 对象。创建文字云后,使用 wc 修改其属性。有关属性列表,请参阅 WordCloudChart 属性。
注意
Text Analytics Toolbox 扩展了 wordcloud (MATLAB®) 函数的功能。它增加了直接使用字符串数组创建文字云的支持,还支持使用词袋模型、N 元词袋模型和 LDA 主题创建文字云。有关 wordcloud (Text Analytics Toolbox) 参考页,请参阅 wordcloud (Text Analytics Toolbox)。
示例
加载示例数据 sonnetsTable。表 tbl 将单词列表包含在变量 Word 中,将相应的频率计数包含在变量 Count 中。
load sonnetsTable
head(tbl) Word Count
___________ _____
{'''tis' } 1
{''Amen'' } 1
{''Fair' } 2
{''Gainst'} 1
{''Since' } 1
{''This' } 2
{''Thou' } 1
{''Thus' } 1
使用 wordcloud 绘制表数据。将单词和相应的单词大小分别指定为 Word 和 Count 变量。
figure wordcloud(tbl,'Word','Count'); title("Sonnets Word Cloud")

如果您安装了 Text Analytics Toolbox™,则可以直接使用字符串数组创建文字云。有关详细信息,请参阅wordcloud (Text Analytics Toolbox)。如果您没有 Text Analytics Toolbox,则必须手动预处理文本数据。
此示例说明如何通过将纯文本读入字符串数组、进行预处理并传递给 wordcloud 函数,使用纯文本创建文字云。
使用 fileread 函数从莎士比亚的十四行诗中读取文本并将其转换为字符串。
sonnets = string(fileread("sonnets.txt")); extractBefore(sonnets,"II")
ans =
"THE SONNETS
by William Shakespeare
I
From fairest creatures we desire increase,
That thereby beauty's rose might never die,
But as the riper should by time decease,
His tender heir might bear his memory:
But thou, contracted to thine own bright eyes,
Feed'st thy light's flame with self-substantial fuel,
Making a famine where abundance lies,
Thy self thy foe, to thy sweet self too cruel:
Thou that art now the world's fresh ornament,
And only herald to the gaudy spring,
Within thine own bud buriest thy content,
And tender churl mak'st waste in niggarding:
Pity the world, or else this glutton be,
To eat the world's due, by the grave and thee.
"
将 sonnets 拆分为其元素包含单个单词的字符串数组。要完成此操作,需要删除所有标点字符,将所有字符串元素合并成一个 1×1 字符串,然后在空白字符处进行拆分。然后,删除少于五个字符的单词并将单词转换为小写。
punctuationCharacters = ["." "?" "!" "," ";" ":"]; sonnets = replace(sonnets,punctuationCharacters," "); words = split(join(sonnets)); words(strlength(words)<5) = []; words = lower(words); words(1:10)
ans = 10×1 string
"sonnets"
"william"
"shakespeare"
"fairest"
"creatures"
"desire"
"increase"
"thereby"
"beauty's"
"might"
将 sonnets 转换为分类数组,然后使用 wordcloud 进行绘图。此函数绘制 C 的唯一元素,大小与这些元素的频率计数对应。
C = categorical(words);
figure
wordcloud(C);
title("Sonnets Word Cloud")
通过将纯文本读入一个字符串数组,对其进行预处理并传递给 wordcloud 函数,即可从纯文本创文字云。
使用 fileread 函数从莎士比亚的十四行诗中读取文本并将其转换为字符串。
sonnets = string(fileread('sonnets.txt')); extractBefore(sonnets,"II")
ans =
"THE SONNETS
by William Shakespeare
I
From fairest creatures we desire increase,
That thereby beauty's rose might never die,
But as the riper should by time decease,
His tender heir might bear his memory:
But thou, contracted to thine own bright eyes,
Feed'st thy light's flame with self-substantial fuel,
Making a famine where abundance lies,
Thy self thy foe, to thy sweet self too cruel:
Thou that art now the world's fresh ornament,
And only herald to the gaudy spring,
Within thine own bud buriest thy content,
And tender churl mak'st waste in niggarding:
Pity the world, or else this glutton be,
To eat the world's due, by the grave and thee.
"
将 sonnets 拆分为其元素包含单个单词的字符串数组。要完成此操作,需要删除所有标点字符,将所有字符串元素合并成一个 1×1 字符串,然后在空白字符处进行拆分。然后,删除少于五个字符的单词并将单词转换为小写。
punctuationCharacters = ["." "?" "!" "," ";" ":"]; sonnets = replace(sonnets,punctuationCharacters," "); words = split(join(sonnets)); words(strlength(words)<5) = []; words = lower(words); words(1:10)
ans = 10×1 string
"sonnets"
"william"
"shakespeare"
"fairest"
"creatures"
"desire"
"increase"
"thereby"
"beauty's"
"might"
查找 sonnets 中的唯一单词并计算它们出现的频率。使用频率计数作为大小数据创建文字云。
[numOccurrences,uniqueWords] = histcounts(categorical(words));
figure
wordcloud(uniqueWords,numOccurrences);
title("Sonnets Word Cloud")
加载示例数据 sonnetsTable。表 tbl 将单词列表包含在变量 Word 中,将相应的频率计数包含在变量 Count 中。
load sonnetsTable
head(tbl) Word Count
___________ _____
{'''tis' } 1
{''Amen'' } 1
{''Fair' } 2
{''Gainst'} 1
{''Since' } 1
{''This' } 2
{''Thou' } 1
{''Thus' } 1
使用 wordcloud 绘制表数据。将单词和相应的单词大小分别指定为 Word 和 Count 变量。要将单词颜色设置为随机值,请将 'Color' 设置为随机矩阵或 RGB 三元组,每一行对应一个单词。
numWords = size(tbl,1); colors = rand(numWords,3); figure wordcloud(tbl,'Word','Count','Color',colors); title("Sonnets Word Cloud")

如果您安装了 Text Analytics Toolbox,则可以直接使用字符串数组创建文字云。如果您没有 Text Analytics Toolbox,则必须手动预处理文本数据。有关如何在没有 Text Analytics Toolbox 的情况下创建文字云的示例,请参阅准备文本数据以创建文字云。
要访问 sonnets.txt 数据集,请使用 openExample 函数打开Create Word Cloud from Text Data (Text Analytics Toolbox)示例。
openExample('textanalytics/CreateWordCloudFromTextDataExample')使用 extractFileText 从 sonnets.txt 中提取文本。
str = extractFileText("sonnets.txt"); extractBefore(str,"II")
ans =
"THE SONNETS
by William Shakespeare
I
From fairest creatures we desire increase,
That thereby beauty's rose might never die,
But as the riper should by time decease,
His tender heir might bear his memory:
But thou, contracted to thine own bright eyes,
Feed'st thy light's flame with self-substantial fuel,
Making a famine where abundance lies,
Thy self thy foe, to thy sweet self too cruel:
Thou that art now the world's fresh ornament,
And only herald to the gaudy spring,
Within thine own bud buriest thy content,
And tender churl mak'st waste in niggarding:
Pity the world, or else this glutton be,
To eat the world's due, by the grave and thee.
"在文字云中显示十四行诗中的单词。
figure wordcloud(str);

输入参数
单词数据的表变量,指定为字符串标量、字符向量、数值索引或逻辑向量。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string
大小数据的表变量,指定为字符串标量、字符向量、数值索引或逻辑向量。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string
输入分类数据,指定为分类数组。此函数绘制 C 的每个唯一元素,其大小对应于 histcounts(C)。
数据类型: categorical
输入单词,指定为字符串向量或字符向量元胞数组。
数据类型: string | cell
单词大小数据,指定为数值向量。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
父容器,指定为 Figure、Panel、Tab、TiledChartLayout 或 GridLayout 对象。
名称-值参数
以 Name1=Value1,...,NameN=ValueN 的形式指定可选参量对组,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。
示例: 'HighlightColor','red' 将高亮颜色设置为红色。
此处所列的 WordCloudChart 属性只是一部分。有关完整列表,请参阅 WordCloudChart 属性。
要显示的最大单词数,指定为非负整数。软件会显示前 MaxDisplayWords 个出现频率最高的单词。
单词颜色,指定为 RGB 三元组、包含颜色名称的字符向量,或者指定为 N×3 矩阵,其中 N 是 WordData 的长度。如果 Color 是矩阵,则每一行对应于 WordData 中相应单词的 RGB 三元组。
RGB 三元组和十六进制颜色代码对于指定自定义颜色非常有用。
RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于
[0,1]范围内,例如[0.4 0.6 0.7]。十六进制颜色代码是字符向量或字符串标量,以井号 (
#) 开头,后跟三个或六个十六进制数字,范围可以是0到F。这些值不区分大小写。因此,颜色代码"#FF8800"与"#ff8800"、"#F80"与"#f80"是等效的。
此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项、等效 RGB 三元组和十六进制颜色代码。
| 颜色名称 | 短名称 | RGB 三元组 | 十六进制颜色代码 | 外观 |
|---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" |
|
"green" | "g" | [0 1 0] | "#00FF00" |
|
"blue" | "b" | [0 0 1] | "#0000FF" |
|
"cyan" | "c" | [0 1 1] | "#00FFFF" |
|
"magenta" | "m" | [1 0 1] | "#FF00FF" |
|
"yellow" | "y" | [1 1 0] | "#FFFF00" |
|
"black" | "k" | [0 0 0] | "#000000" |
|
"white" | "w" | [1 1 1] | "#FFFFFF" |
|
下表列出了浅色和深色主题中绘图的默认调色板。
| 调色板 | 调色板颜色 |
|---|---|
在 R2025a 之前的版本中: 大多数绘图默认使用这些颜色。 |
|
|
|
您可以使用 orderedcolors 和 rgb2hex 函数获取这些调色板的 RGB 三元组和十六进制颜色代码。例如,获取 "gem" 调色板的 RGB 三元组并将其转换为十六进制颜色代码。
RGB = orderedcolors("gem");
H = rgb2hex(RGB);在 R2023b 之前的版本中: 使用 RGB = get(groot,"FactoryAxesColorOrder") 获取 RGB 三元组。
在 R2024a 之前的版本中: 使用 H = compose("#%02X%02X%02X",round(RGB*255)) 获取十六进制颜色代码。
示例: 'blue'
示例: [0 0 1]
单词高亮颜色,指定为 RGB 三元组或包含颜色名称的字符向量。软件使用此颜色突出显示那些最大的单词。
RGB 三元组和十六进制颜色代码对于指定自定义颜色非常有用。
RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于
[0,1]范围内,例如[0.4 0.6 0.7]。十六进制颜色代码是字符向量或字符串标量,以井号 (
#) 开头,后跟三个或六个十六进制数字,范围可以是0到F。这些值不区分大小写。因此,颜色代码"#FF8800"与"#ff8800"、"#F80"与"#f80"是等效的。
此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项、等效 RGB 三元组和十六进制颜色代码。
| 颜色名称 | 短名称 | RGB 三元组 | 十六进制颜色代码 | 外观 |
|---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" |
|
"green" | "g" | [0 1 0] | "#00FF00" |
|
"blue" | "b" | [0 0 1] | "#0000FF" |
|
"cyan" | "c" | [0 1 1] | "#00FFFF" |
|
"magenta" | "m" | [1 0 1] | "#FF00FF" |
|
"yellow" | "y" | [1 1 0] | "#FFFF00" |
|
"black" | "k" | [0 0 0] | "#000000" |
|
"white" | "w" | [1 1 1] | "#FFFFFF" |
|
下表列出了浅色和深色主题中绘图的默认调色板。
| 调色板 | 调色板颜色 |
|---|---|
在 R2025a 之前的版本中: 大多数绘图默认使用这些颜色。 |
|
|
|
您可以使用 orderedcolors 和 rgb2hex 函数获取这些调色板的 RGB 三元组和十六进制颜色代码。例如,获取 "gem" 调色板的 RGB 三元组并将其转换为十六进制颜色代码。
RGB = orderedcolors("gem");
H = rgb2hex(RGB);在 R2023b 之前的版本中: 使用 RGB = get(groot,"FactoryAxesColorOrder") 获取 RGB 三元组。
在 R2024a 之前的版本中: 使用 H = compose("#%02X%02X%02X",round(RGB*255)) 获取十六进制颜色代码。
示例: 'blue'
示例: [0 0 1]
文字云图的形状,指定为 'oval' 或 'rectangle'。
示例: 'rectangle'
单词的位置布局,指定为非负整数。如果您使用相同的输入重复调用 wordcloud,则每次的单词位置布局都相同。要获得不同的单词位置布局,请使用不同的 LayoutNum 值。
输出参量
WordCloudChart 对象。创建 WordCloudChart 后可以修改其属性。有关详细信息,请参阅 WordCloudChart 属性。
提示
Text Analytics Toolbox 扩展了 wordcloud (MATLAB) 函数的功能。它增加了直接使用字符串数组创建文字云的支持,还支持使用词袋模型、N 元词袋模型和 LDA 主题创建文字云。有关 wordcloud (Text Analytics Toolbox) 参考页,请参阅 wordcloud (Text Analytics Toolbox)。
扩展功能
wordcloud 函数支持 tall 数组,但存在以下使用说明和限制:
不支持语法
wc = wordcloud(str),其中str为字符串数组、字符向量或字符向量元胞数组(这些输入需要 Text Analytics Toolbox)。当提供 tall 数组形式的
words和sizedata输入时,它们将被收集到内存中,因此必须能放入内存中。
版本历史记录
在 R2017b 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)









