Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

wordcloud

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

  • Word cloud chart from text data

说明

示例

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 sonnetsTable
head(tbl)
       Word        Count
    ___________    _____

    {'''tis'  }      1  
    {''Amen'' }      1  
    {''Fair'  }      2  
    {''Gainst'}      1  
    {''Since' }      1  
    {''This'  }      2  
    {''Thou'  }      1  
    {''Thus'  }      1  

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

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")

Figure contains an object of type wordcloud. The chart of type wordcloud has 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 绘制表数据。将单词和相应的单词大小分别指定为 WordCount 变量。要将单词颜色设置为随机值,请将 '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 的情况下创建文字云的示例,请参阅准备文本数据以创建文字云

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

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

figure
wordcloud(str);

输入参数

全部折叠

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

数据类型: table

单词数据的表变量,指定为字符串标量、字符向量、数值索引或逻辑向量。

数据类型: 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

父容器,指定为 FigurePanelTabTiledChartLayoutGridLayout 对象。

名称-值参数

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

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

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

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

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

单词颜色,指定为 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"

Sample of the color red

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

Sample of the color green

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

Sample of the color blue

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

Sample of the color cyan

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

Sample of the color magenta

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

Sample of the color yellow

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

Sample of the color black

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

Sample of the color white

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

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

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

示例: 'blue'

示例: [0 0 1]

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

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

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

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

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

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

Sample of the color red

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

Sample of the color green

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

Sample of the color blue

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

Sample of the color cyan

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

Sample of the color magenta

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

Sample of the color yellow

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

Sample of the color black

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

Sample of the color white

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

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

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

示例: '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)

扩展功能

版本历史记录

在 R2017b 中推出