This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Plotting Categorical Data

This example shows how to visualize categorical data. The categorical array is used to store data with values from a finite set of discrete categories. A subset of charting types support categorical arrays.

Create Categorical Data

Discrete values are often represented as character vectors. You can convert a cell array of character vectors to a categorical array using the categorical function.

load patients SelfAssessedHealthStatus Height Weight     % load data

HealthStatus = categorical(SelfAssessedHealthStatus);    % convert to categorical
summary(HealthStatus)                                    % show summary of categorical data
     Excellent      34 
     Fair           15 
     Good           40 
     Poor           11 

Plot Pie Chart with Categorical Data

Pie charts are often used to visualize data consisting of discrete values. Slices are automatically labeled with category names and percentages when using the pie function with categorical arrays.

title('Self Assessed Health Status')

To offset slices in the pie chart, specify the name of the categories as an optional input argument.

title('Self Assessed Health Status')

Plot Histogram with Categorical Data

Histograms can be used to plot the counts for each discrete value in a categorical array. The bars are automatically labeled with category names.

title('Self Assessed Health Status')

Plot Scatter Plot Colored by Categorical Data

You can also use categorical data in scatter plots. You can use the categorical array to color the data by category.


Was this topic helpful?