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 = 10x1 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,则必须手动预处理文本数据。有关如何在没有 Text Analytics Toolbox 的情况下创建文字云的示例,请参阅准备文本数据以创建文字云。
使用 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);
输入参数
wordVar
— 单词数据的表变量
字符串标量 | 字符向量 | 数值索引 | 逻辑向量
单词数据的表变量,指定为字符串标量、字符向量、数值索引或逻辑向量。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
sizeVar
— 大小数据的表变量
字符串标量 | 字符向量 | 数值索引 | 逻辑向量
大小数据的表变量,指定为字符串标量、字符向量、数值索引或逻辑向量。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
C
— 输入分类数据
分类数组
输入分类数据,指定为分类数组。此函数绘制 C
的每个唯一元素,其大小对应于 histcounts(C)
。
数据类型: categorical
words
— 输入单词
字符串向量 | 字符向量元胞数组
输入单词,指定为字符串向量或字符向量元胞数组。
数据类型: string
| cell
sizeData
— 单词大小数据
数值向量
单词大小数据,指定为数值向量。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
parent
— 父容器
Figure
对象 | Panel
对象 | Tab
对象 | TiledChartLayout
对象 | GridLayout
对象
父容器,指定为 Figure
、Panel
、Tab
、TiledChartLayout
或 GridLayout
对象。
名称-值参数
将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参数名称,Value
是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: 'HighlightColor','red'
将高亮颜色设置为红色。
此处所列的 WordCloudChart
属性只是一部分。有关完整列表,请参阅 WordCloudChart 属性。
MaxDisplayWords
— 要显示的最大单词数
100 (默认) | 非负整数
要显示的最大单词数,指定为非负整数。软件会显示前 MaxDisplayWords
个出现频率最高的单词。
Color
— 单词颜色
[0.2510 0.2510 0.2510]
(默认) | RGB 三元组 | 包含颜色名称的字符向量 | 矩阵
单词颜色,指定为 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" |
以下是 MATLAB 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。
RGB 三元组 | 十六进制颜色代码 | 外观 |
---|---|---|
[0 0.4470 0.7410] | "#0072BD" | |
[0.8500 0.3250 0.0980] | "#D95319" | |
[0.9290 0.6940 0.1250] | "#EDB120" | |
[0.4940 0.1840 0.5560] | "#7E2F8E" | |
[0.4660 0.6740 0.1880] | "#77AC30" | |
[0.3010 0.7450 0.9330] | "#4DBEEE" | |
[0.6350 0.0780 0.1840] | "#A2142F" |
示例: 'blue'
示例: [0 0 1]
HighlightColor
— 单词高亮颜色
[0.8510 0.3255 0.0980]
(默认) | RGB 三元组 | 包含颜色名称的字符向量
单词高亮颜色,指定为 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" |
以下是 MATLAB 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。
RGB 三元组 | 十六进制颜色代码 | 外观 |
---|---|---|
[0 0.4470 0.7410] | "#0072BD" | |
[0.8500 0.3250 0.0980] | "#D95319" | |
[0.9290 0.6940 0.1250] | "#EDB120" | |
[0.4940 0.1840 0.5560] | "#7E2F8E" | |
[0.4660 0.6740 0.1880] | "#77AC30" | |
[0.3010 0.7450 0.9330] | "#4DBEEE" | |
[0.6350 0.0780 0.1840] | "#A2142F" |
示例: 'blue'
示例: [0 0 1]
Shape
— 文字云的形状
'oval'
(默认) | 'rectangle'
文字云图的形状,指定为 'oval'
或 'rectangle'
。
示例: 'rectangle'
LayoutNum
— 单词的位置布局
1 (默认) | 非负整数
单词的位置布局,指定为非负整数。如果您使用相同的输入重复调用 wordcloud
,则每次的单词位置布局都相同。要获得不同的单词位置布局,请使用不同的 LayoutNum
值。
输出参数
wc
— WordCloudChart
对象
WordCloudChart
对象
WordCloudChart
对象。创建 WordCloudChart
后可以修改其属性。有关详细信息,请参阅 WordCloudChart 属性。
提示
Text Analytics Toolbox 扩展了 wordcloud
(MATLAB) 函数的功能。它增加了直接使用字符串数组创建文字云的支持,还支持使用词袋模型、N 元词袋模型和 LDA 主题创建文字云。有关 wordcloud
(Text Analytics Toolbox) 参考页,请参阅 wordcloud
(Text Analytics Toolbox)。
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
用法说明和限制:
不支持语法
wc = wordcloud(str)
,其中str
为字符串数组、字符向量或字符向量元胞数组(这些输入需要 Text Analytics Toolbox)。当提供 tall 数组形式的
words
和sizedata
输入时,它们将被收集到内存中,因此必须能放入内存中。
版本历史记录
在 R2017b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)