wordcloud
R2026bテキスト データからワード クラウド チャートを作成

構文
説明
wordcloud(___, は、名前と値のペアの引数を 1 つ以上使用して、追加の Name,Value)WordCloudChart プロパティを指定します。
wordcloud( は、parent,___)parent で指定された Figure、パネルまたはタブにワード クラウドを作成します。
は、wc = wordcloud(___)WordCloudChart オブジェクトを返します。ワード クラウドのプロパティを作成後に変更するには、wc を使用します。プロパティの一覧については、WordCloudChart のプロパティ を参照してください。
メモ
Text Analytics Toolbox は、MATLAB® 関数 wordcloud の機能を拡張します。これにより、string 配列からのワード クラウドの直接作成、および bag of words モデル、bag of n gram モデル、LDA トピックからのワード クラウドの作成のサポートが追加されます。wordcloud (Text Analytics Toolbox) リファレンス ページについては、wordcloud (Text Analytics Toolbox) を参照してください。
例
サンプル データ sonnetsTable を読み込みます。table 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
table データを wordcloud を使用してプロットします。単語と対応するワード サイズを変数 Word と変数 Count にそれぞれ指定します。
figure wordcloud(tbl,'Word','Count'); title("Sonnets Word Cloud")

Text Analytics Toolbox™ がインストールされている場合は、string 配列から直接ワード クラウドを作成できます。詳細については、wordcloud (Text Analytics Toolbox)を参照してください。Text Analytics Toolbox がない場合は、テキスト データの前処理を手動で行わなければなりません。
この例では、プレーン テキストから string 配列に読み取り、前処理して、それを関数 wordcloud に渡して、ワード クラウドを作成する方法を示します。
関数 fileread を使用して、シェークスピアのソネットからテキストを読み取り、string に変換します。
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 を、個々の単語を要素として含む string 配列に分割します。これを行うには、句読点を削除し、すべての string 要素を 1 行 1 列の string に結合してから、空白文字の位置で分割します。その後、5 文字未満の単語を削除し、単語を小文字に変換します。
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 を categorical 配列に変換して、wordcloud を使用してプロットします。この関数は C の一意の要素を、その頻度数に対応するサイズでプロットします。
C = categorical(words);
figure
wordcloud(C);
title("Sonnets Word Cloud")
プレーン テキストから string 配列に読み取り、前処理して、それを関数 wordcloud に渡して、ワード クラウドを作成します。
関数 fileread を使用して、シェークスピアのソネットからテキストを読み取り、string に変換します。
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 を、個々の単語を要素として含む string 配列に分割します。これを行うには、句読点を削除し、すべての string 要素を 1 行 1 列の string に結合してから、空白文字の位置で分割します。その後、5 文字未満の単語を削除し、単語を小文字に変換します。
punctuationCharacters = ["." "?" "!" "," ";" ":"]; sonnets = replace(sonnets,punctuationCharacters," "); words = split(join(sonnets)); words(strlength(words)<5) = []; words = lower(words); words(1:10)
ans = 10×1 string array
"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 を読み込みます。table 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
table データを wordcloud を使用してプロットします。単語と対応するワード サイズを変数 Word と変数 Count にそれぞれ指定します。単語の色を乱数値に設定するには、'Color' を、単語ごとに 1 つの行をもつ乱数行列または RGB 3 成分に設定します。
numWords = size(tbl,1); colors = rand(numWords,3); figure wordcloud(tbl,'Word','Count','Color',colors); title("Sonnets Word Cloud")

Text Analytics Toolbox がインストールされている場合は、string 配列から直接ワード クラウドを作成できます。Text Analytics Toolbox がない場合は、テキスト データの前処理を手動で行わなければなりません。Text Analytics Toolbox なしでワード クラウドを作成する方法の例については、ワード クラウドのテキスト データの準備を参照してください。
sonnets.txt データセットにアクセスするには、openExample 関数を使用してテキスト データからのワード クラウドの作成 (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);

入力引数
単語データの table 変数。string スカラー、文字ベクトル、数値インデックス、または logical ベクトルとして指定します。
データ型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string
サイズ データの table 変数。string スカラー、文字ベクトル、数値インデックス、または logical ベクトルとして指定します。
データ型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string
入力 categorical データ。categorical 配列として指定します。この関数は、C の一意の各要素を、histcounts(C) に対応するサイズでプロットします。
データ型: categorical
入力語。string ベクトル、または文字ベクトルの cell 配列として指定します。
データ型: 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 3 成分、色名を含む文字ベクトル、または N 行 3 列の行列 (N は WordData の長さ) として指定します。Color が行列の場合、各行は WordData の対応する単語の RGB 3 成分に対応します。
RGB 3 成分および 16 進数カラー コードは、カスタム色を指定するのに役立ちます。
RGB 3 成分は、色の赤、緑、青成分の強度を指定する 3 成分の行ベクトルです。強度値は
[0,1]の範囲でなければなりません。たとえば[0.4 0.6 0.7]のようになります。16 進数カラー コードは、ハッシュ記号 (
#) で始まり、3 桁または 6 桁の0からFまでの範囲の 16 進数が続く文字ベクトルまたは string スカラーです。この値は大文字と小文字を区別しません。したがって、カラー コード"#FF8800"、"#ff8800"、"#F80"、および"#f80"は等価です。
あるいは、名前を使用して一部の一般的な色を指定できます。次の表に、名前の付いた色オプション、等価の RGB 3 成分、および 16 進数カラー コードを示します。
| 色名 | 省略名 | RGB 3 成分 | 16 進数カラー コード | 外観 |
|---|---|---|---|---|
"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 3 成分および 16 進数カラー コードを取得できます。たとえば、"gem" パレットの RGB 3 成分を取得し、16 進数カラー コードに変換します。
RGB = orderedcolors("gem");
H = rgb2hex(RGB);R2024a より前: H = compose("#%02X%02X%02X",round(RGB*255)) を使用して、16 進数カラー コードを取得します。
R2023b より前: RGB = get(groot,"FactoryAxesColorOrder") を使用して、RGB 3 成分を取得します。
例: 'blue'
例: [0 0 1]
単語の強調表示の色。RGB 3 成分、または色名を含む文字ベクトルとして指定します。サイズが最も大きい単語がこの色で強調表示されます。
RGB 3 成分および 16 進数カラー コードは、カスタム色を指定するのに役立ちます。
RGB 3 成分は、色の赤、緑、青成分の強度を指定する 3 成分の行ベクトルです。強度値は
[0,1]の範囲でなければなりません。たとえば[0.4 0.6 0.7]のようになります。16 進数カラー コードは、ハッシュ記号 (
#) で始まり、3 桁または 6 桁の0からFまでの範囲の 16 進数が続く文字ベクトルまたは string スカラーです。この値は大文字と小文字を区別しません。したがって、カラー コード"#FF8800"、"#ff8800"、"#F80"、および"#f80"は等価です。
あるいは、名前を使用して一部の一般的な色を指定できます。次の表に、名前の付いた色オプション、等価の RGB 3 成分、および 16 進数カラー コードを示します。
| 色名 | 省略名 | RGB 3 成分 | 16 進数カラー コード | 外観 |
|---|---|---|---|---|
"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 3 成分および 16 進数カラー コードを取得できます。たとえば、"gem" パレットの RGB 3 成分を取得し、16 進数カラー コードに変換します。
RGB = orderedcolors("gem");
H = rgb2hex(RGB);R2024a より前: H = compose("#%02X%02X%02X",round(RGB*255)) を使用して、16 進数カラー コードを取得します。
R2023b より前: RGB = get(groot,"FactoryAxesColorOrder") を使用して、RGB 3 成分を取得します。
例: 'blue'
例: [0 0 1]
ワード クラウド チャートの形状。'oval' または 'rectangle' として指定します。
例: 'rectangle'
単語配置レイアウト。非負の整数として指定します。同じ入力を使用して wordcloud を繰り返し呼び出すと、単語配置レイアウトは毎回同じになります。別の単語配置レイアウトを取得するには、異なる LayoutNum 値を使用します。
出力引数
WordCloudChart オブジェクト。WordCloudChart の作成後にプロパティを変更することができます。詳細については、WordCloudChart のプロパティ を参照してください。
ヒント
Text Analytics Toolbox は、MATLAB 関数 wordcloud の機能を拡張します。これにより、string 配列からのワード クラウドの直接作成、および bag of words モデル、bag of n gram モデル、LDA トピックからのワード クラウドの作成のサポートが追加されます。wordcloud (Text Analytics Toolbox) リファレンス ページについては、wordcloud (Text Analytics Toolbox) を参照してください。
拡張機能
wordcloud 関数は tall 配列をサポートしていますが、以下の使用上の注意および制限があります。
構文
wc = wordcloud(str)はサポートされていません。strは string 配列、文字ベクトル、または文字ベクトルの cell 配列です (これらの入力には Text Analytics Toolbox が必要です)。wordsおよびsizedataの入力が tall 配列として指定されると、まとめてメモリに格納されるため、メモリに収まらなければなりません。
バージョン履歴
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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)









