?文字云图 - MATLAB wordcloud MathWorks 中国 (2024)

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

全页折叠

  • º文字云图 - MATLAB wordcloud MathWorks 中国 (1)

º文字云图 - MATLAB wordcloud MathWorks 中国 (2)

语法

wordcloud(tbl,wordVar,sizeVar)

wordcloud(words,sizeData)

wordcloud(C)

wordcloud(___,Name,Value)

wordcloud(parent,___)

wc = wordcloud(___)

说明

示例

wordcloud(tbl,wordVar,sizeVar) 根据表 tbl 创建文字云图。表中的变量 wordVarsizeVar 分别指定单词和单词大小。

示例

wordcloud(words,sizeData) 使用 words 的元素(单词大小由 SizeData 指定)创建文字云图。

示例

wordcloud(C) 根据分类数组 C 的唯一元素创建文字云图,大小与这些元素的频率计数对应。如果您拥有 Text Analytics Toolbox™,则 C 可以是字符串数组、字符向量或字符向量元胞数组。

示例

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 sonnetsTablehead(tbl)
 Word Count ___________ _____ {'''tis' } 1 {''Amen'' } 1 {''Fair' } 2 {''Gainst'} 1 {''Since' } 1 {''This' } 2 {''Thou' } 1 {''Thus' } 1 

使用 wordcloud 绘制表数据。将单词和相应的单词大小分别指定为 WordCount 变量。

figurewordcloud(tbl,'Word','Count');title("Sonnets Word Cloud")

º文字云图 - MATLAB wordcloud MathWorks 中国 (3)

准备文本数据以创建文字云

如果您安装了 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 nigg*rding: 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);figurewordcloud(C);title("Sonnets Word Cloud")

º文字云图 - MATLAB wordcloud MathWorks 中国 (4)

指定单词大小

打开实时脚本

通过将纯文本读入一个字符串数组,对其进行预处理并传递给 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 nigg*rding: 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));figurewordcloud(uniqueWords,numOccurrences);title("Sonnets Word Cloud")

º文字云图 - MATLAB wordcloud MathWorks 中国 (5)

指定单词颜色

打开实时脚本

加载示例数据 sonnetsTable。表 tbl 将单词列表包含在变量 Word 中,将相应的频率计数包含在变量 Count 中。

load sonnetsTablehead(tbl)
 Word Count ___________ _____ {'''tis' } 1 {''Amen'' } 1 {''Fair' } 2 {''Gainst'} 1 {''Since' } 1 {''This' } 2 {''Thou' } 1 {''Thus' } 1 

使用 wordcloud 绘制表数据。将单词和相应的单词大小分别指定为 WordCount 变量。要将单词颜色设置为随机值,请将 'Color' 设置为随机矩阵或 RGB 三元组,每一行对应一个单词。

numWords = size(tbl,1);colors = rand(numWords,3);figurewordcloud(tbl,'Word','Count','Color',colors);title("Sonnets Word Cloud")

º文字云图 - MATLAB wordcloud MathWorks 中国 (6)

使用 Text Analytics Toolbox 创建文字云

如果您安装了 Text Analytics Toolbox,则可以直接使用字符串数组创建文字云。如果您没有 Text Analytics Toolbox,则必须手动预处理文本数据。有关如何在没有 Text Analytics Toolbox 的情况下创建文字云的示例,请参阅准备文本数据以创建文字云

使用 extractFileTextsonnets.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 nigg*rding: Pity the world, or else this glutton be, To eat the world's due, by the grave and thee. "

在文字云中显示十四行诗中的单词。

figurewordcloud(str);

º文字云图 - MATLAB wordcloud MathWorks 中国 (7)

输入参数

全部折叠

tbl输入表

输入表,在表列中指定单词和单词大小。分别在 wordVarsizeVar 输入参量给出的变量中指定单词和相应的单词大小。

数据类型: table

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 对象

父容器,指定为 FigurePanelTabTiledChartLayoutGridLayout 对象。

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: 'HighlightColor','red' 将高亮颜色设置为红色。

此处所列的 WordCloudChart 属性只是一部分。有关完整列表,请参阅 WordCloudChart 属性

MaxDisplayWords要显示的最大单词数
100 (默认) | 非负整数

要显示的最大单词数,指定为非负整数。软件会显示前 MaxDisplayWords 个出现频率最高的单词。

Color单词颜色
[0.3804 0.3804 0.3804] (默认) | RGB 三元组 | 包含颜色名称的字符向量 | 矩阵

单词颜色,指定为 RGB 三元组、包含颜色名称的字符向量,或者指定为 N×3 矩阵,其中 NWordData 的长度。如果 Color 是矩阵,则每一行对应于 WordData 中相应单词的 RGB 三元组。

RGB 三元组和十六进制颜色代码对于指定自定义颜色非常有用。

  • RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于 [0,1] 范围内,例如 [0.4 0.6 0.7]

  • 十六进制颜色代码是字符向量或字符串标量,以井号 (#) 开头,后跟三个或六个十六进制数字,范围可以是 0F。这些值不区分大小写。因此,颜色代码 "#FF8800""#ff8800""#F80""#f80" 是等效的。

此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项、等效 RGB 三元组和十六进制颜色代码。

颜色名称短名称RGB 三元组十六进制颜色代码外观
"red""r"[1 0 0]"#FF0000"

º文字云图 - MATLAB wordcloud MathWorks 中国 (8)

"green""g"[0 1 0]"#00FF00"

º文字云图 - MATLAB wordcloud MathWorks 中国 (9)

"blue""b"[0 0 1]"#0000FF"

º文字云图 - MATLAB wordcloud MathWorks 中国 (10)

"cyan" "c"[0 1 1]"#00FFFF"

º文字云图 - MATLAB wordcloud MathWorks 中国 (11)

"magenta""m"[1 0 1]"#FF00FF"

º文字云图 - MATLAB wordcloud MathWorks 中国 (12)

"yellow""y"[1 1 0]"#FFFF00"

º文字云图 - MATLAB wordcloud MathWorks 中国 (13)

"black""k"[0 0 0]"#000000"

º文字云图 - MATLAB wordcloud MathWorks 中国 (14)

"white""w"[1 1 1]"#FFFFFF"

º文字云图 - MATLAB wordcloud MathWorks 中国 (15)

以下是 MATLAB 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。

RGB 三元组十六进制颜色代码外观
[0 0.4470 0.7410]"#0072BD"

º文字云图 - MATLAB wordcloud MathWorks 中国 (16)

[0.8500 0.3250 0.0980]"#D95319"

º文字云图 - MATLAB wordcloud MathWorks 中国 (17)

[0.9290 0.6940 0.1250]"#EDB120"

º文字云图 - MATLAB wordcloud MathWorks 中国 (18)

[0.4940 0.1840 0.5560]"#7E2F8E"

º文字云图 - MATLAB wordcloud MathWorks 中国 (19)

[0.4660 0.6740 0.1880]"#77AC30"

º文字云图 - MATLAB wordcloud MathWorks 中国 (20)

[0.3010 0.7450 0.9330]"#4DBEEE"

º文字云图 - MATLAB wordcloud MathWorks 中国 (21)

[0.6350 0.0780 0.1840]"#A2142F"

º文字云图 - MATLAB wordcloud MathWorks 中国 (22)

示例: 'blue'

示例: [0 0 1]

HighlightColor单词高亮颜色
[0.7529 0.2980 0.0431] (默认) | RGB 三元组 | 包含颜色名称的字符向量

单词高亮颜色,指定为 RGB 三元组或包含颜色名称的字符向量。软件使用此颜色突出显示那些最大的单词。

RGB 三元组和十六进制颜色代码对于指定自定义颜色非常有用。

  • RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于 [0,1] 范围内,例如 [0.4 0.6 0.7]

  • 十六进制颜色代码是字符向量或字符串标量,以井号 (#) 开头,后跟三个或六个十六进制数字,范围可以是 0F。这些值不区分大小写。因此,颜色代码 "#FF8800""#ff8800""#F80""#f80" 是等效的。

此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项、等效 RGB 三元组和十六进制颜色代码。

颜色名称短名称RGB 三元组十六进制颜色代码外观
"red""r"[1 0 0]"#FF0000"

º文字云图 - MATLAB wordcloud MathWorks 中国 (23)

"green""g"[0 1 0]"#00FF00"

º文字云图 - MATLAB wordcloud MathWorks 中国 (24)

"blue""b"[0 0 1]"#0000FF"

º文字云图 - MATLAB wordcloud MathWorks 中国 (25)

"cyan" "c"[0 1 1]"#00FFFF"

º文字云图 - MATLAB wordcloud MathWorks 中国 (26)

"magenta""m"[1 0 1]"#FF00FF"

º文字云图 - MATLAB wordcloud MathWorks 中国 (27)

"yellow""y"[1 1 0]"#FFFF00"

º文字云图 - MATLAB wordcloud MathWorks 中国 (28)

"black""k"[0 0 0]"#000000"

º文字云图 - MATLAB wordcloud MathWorks 中国 (29)

"white""w"[1 1 1]"#FFFFFF"

º文字云图 - MATLAB wordcloud MathWorks 中国 (30)

以下是 MATLAB 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。

RGB 三元组十六进制颜色代码外观
[0 0.4470 0.7410]"#0072BD"

º文字云图 - MATLAB wordcloud MathWorks 中国 (31)

[0.8500 0.3250 0.0980]"#D95319"

º文字云图 - MATLAB wordcloud MathWorks 中国 (32)

[0.9290 0.6940 0.1250]"#EDB120"

º文字云图 - MATLAB wordcloud MathWorks 中国 (33)

[0.4940 0.1840 0.5560]"#7E2F8E"

º文字云图 - MATLAB wordcloud MathWorks 中国 (34)

[0.4660 0.6740 0.1880]"#77AC30"

º文字云图 - MATLAB wordcloud MathWorks 中国 (35)

[0.3010 0.7450 0.9330]"#4DBEEE"

º文字云图 - MATLAB wordcloud MathWorks 中国 (36)

[0.6350 0.0780 0.1840]"#A2142F"

º文字云图 - MATLAB wordcloud MathWorks 中国 (37)

示例: 'blue'

示例: [0 0 1]

Shape文字云的形状
'oval' (默认) | 'rectangle'

文字云图的形状,指定为 'oval''rectangle'

示例: 'rectangle'

LayoutNum单词的位置布局
1 (默认) | 非负整数

单词的位置布局,指定为非负整数。如果您使用相同的输入重复调用 wordcloud,则每次的单词位置布局都相同。要获得不同的单词位置布局,请使用不同的 LayoutNum 值。

输出参量

全部折叠

wcWordCloudChart 对象
WordCloudChart 对象

WordCloudChart 对象。创建 WordCloudChart 后可以修改其属性。有关详细信息,请参阅 WordCloudChart 属性

提示

Text Analytics Toolbox 扩展了 wordcloud (MATLAB) 函数的功能。它增加了直接使用字符串数组创建文字云的支持,还支持使用词袋模型、N 元词袋模型和 LDA 主题创建文字云。有关 wordcloud (Text Analytics Toolbox) 参考页,请参阅 wordcloud (Text Analytics Toolbox)

扩展功能

版本历史记录

在 R2017b 中推出

另请参阅

string | split | join | replace | lower | splitlines | wordcloud (Text Analytics Toolbox)

主题

  • 分析字符串数组的文本数据
  • 创建字符串数组
  • 搜索和替换文本
  • 比较文本
  • 测试空字符串和缺失值

MATLAB 命令

您点击的链接对应于以下 MATLAB 命令:

 

请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。

º文字云图 - MATLAB wordcloud MathWorks 中国 (38)

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:

Americas

Europe

Asia Pacific

Contact your local office

?文字云图 - MATLAB wordcloud MathWorks 中国 (2024)

References

Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 6347

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.