Main Content

Visualize Text Data Using Word Clouds

This example shows how to visualize text data using word clouds.

Text Analytics Toolbox extends the functionality of thewordcloud(MATLAB) function. It adds support for creating word clouds directly from string arrays and creating word clouds from bag-of-words models and LDA topics.

Load the example data. The filefactoryReports.csvcontains factory reports, including a text description and categorical labels for each event.

文件名="factoryReports.csv"; tbl = readtable(filename,'TextType','string');

Extract the text data from theDescriptioncolumn.

textData = tbl.Description; textData(1:10)
ans =10x1 string"Items are occasionally getting stuck in the scanner spools." "Loud rattling and banging sounds are coming from assembler pistons." "There are cuts to the power when starting the plant." "Fried capacitors in the assembler." "Mixer tripped the fuses." "Burst pipe in the constructing agent is spraying coolant." "A fuse is blown in the mixer." "Things continue to tumble off of the belt." "Falling items from the conveyor belt." "The scanner reel is split, it will soon begin to curve."

Create a word cloud from the reports.

figure wordcloud(textData); title("Factory Reports")

图包含一个wordcloud类型的对象。chart of type wordcloud has title Factory Reports.

Compare the words in the reports with labels"Leak"and"Mechanical Failure". Create word clouds of the reports for each of these labels. Specify the word colors to be blue and magenta for each word cloud respectively.

figure labels = tbl.Category; subplot(1,2,1) idx = labels =="Leak"; wordcloud(textData(idx),'Color','blue'); title("Leak") subplot(1,2,2) idx = labels =="Mechanical Failure"; wordcloud(textData(idx),'Color','magenta'); title("Mechanical Failure")

Figure contains objects of type wordcloud. The chart of type wordcloud has title Leak. The chart of type wordcloud has title Mechanical Failure.

Compare the words in the reports with urgency "Low", "Medium", and "High".

figure urgency = tbl.Urgency; subplot(1,3,1) idx = urgency =="Low"; wordcloud(textData(idx)); title("Urgency: Low") subplot(1,3,2) idx = urgency =="Medium"; wordcloud(textData(idx)); title("Urgency: Medium") subplot(1,3,3) idx = urgency =="High"; wordcloud(textData(idx)); title("Urgency: High")

Figure contains objects of type wordcloud. The chart of type wordcloud has title Urgency: Low. The chart of type wordcloud has title Urgency: Medium. The chart of type wordcloud has title Urgency: High.

Compare the words in the reports with cost reported in hundreds of dollars to the reports with costs reported in thousands of dollars. Create word clouds of the reports for each of these amounts with highlight color blue and red respectively.

cost = tbl.Cost; idx = cost > 100; figure wordcloud(textData(idx),'HighlightColor','blue'); title("Cost > $100")

图包含一个wordcloud类型的对象。chart of type wordcloud has title Cost > $100.

idx = cost > 1000; figure wordcloud(textData(idx),'HighlightColor','red'); title("Cost > $1,000")

图包含一个wordcloud类型的对象。chart of type wordcloud has title Cost > $1,000.

See Also

||

Related Topics