www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/PlotCategoricalDataExample.m

    %% Plot Categorical Data
% This example shows how to plot data from a categorical array.   
%% Load Sample Data
% Load sample data gathered from 100 patients. 

load patients

whos
%% Create Categorical Arrays from Cell Arrays of Character Vectors
% The workspace variable, |Location|, is a cell array of character vectors that contains  
% the three unique medical facilities where patients were observed. 
% 
% To access and compare data more easily, convert |Location| to a categorical  
% array. 

Location = categorical(Location);
%% 
% Summarize the categorical array. 

summary(Location)
%% 
% 39 patients were observed at County General Hospital, 24 at St. Mary's  
% Medical Center, and 37 at the VA Hospital.  
% 
% The workspace variable, |SelfAssessedHealthStatus|, contains four unique  
% values, |Excellent|, |Fair|, |Good|, and |Poor|. 
% 
% Convert |SelfAssessedHealthStatus| to an ordinal categorical array, such  
% that the categories have the mathematical ordering |Poor < Fair < Good < Excellent|. 

SelfAssessedHealthStatus = categorical(SelfAssessedHealthStatus,...
    {'Poor' 'Fair' 'Good' 'Excellent'},'Ordinal',true);
%% 
% Summarize the categorical array, |SelfAssessedHealthStatus|. 

summary(SelfAssessedHealthStatus)
%% Plot Histogram
% Create a histogram bar plot directly from a categorical array. 

figure
histogram(SelfAssessedHealthStatus)
title('Self Assessed Health Status From 100 Patients')
%% 
% The function |hist| accepts the categorical array, |SelfAssessedHealthStatus|,  
% and plots the category counts for each of the four categories.  
% 
% Create a histogram of the hospital location for only the patients who assessed  
% their health as |Fair| or |Poor|. 

figure
histogram(Location(SelfAssessedHealthStatus<='Fair'))
title('Location of Patients in Fair or Poor Health')
%% Create Pie Chart
% Create a pie chart directly from a categorical array. 

figure
pie(SelfAssessedHealthStatus);
title('Self Assessed Health Status From 100 Patients')
%% 
% The function |pie| accepts the categorical array, |SelfAssessedHealthStatus|, 
% and plots a pie chart of the four categories.
%% Create Pareto Chart
% Create a Pareto chart from the category counts for each of the four
% categories of |SelfAssessedHealthStatus|.

figure
A = countcats(SelfAssessedHealthStatus);
C = categories(SelfAssessedHealthStatus);
pareto(A,C);
title('Self Assessed Health Status From 100 Patients')

%% 
% The first input argument to |pareto| must be a vector. If a categorical 
% array is a matrix or multidimensional array, reshape it into a vector 
% before calling |countcats| and |pareto|.

%% Create Scatter Plot
% Convert the cell array of character vectors to a categorical array. 

Gender = categorical(Gender);
%% 
% Summarize the categorical array, |Gender|. 

summary(Gender)
%% 
% |Gender| is a 100-by-1 categorical array with two categories, |Female|  
% and |Male|.  
% 
% Use the categorical array, |Gender|, to access |Weight| and |Height| data  
% for each gender separately. 

X1 = Weight(Gender=='Female');
Y1 = Height(Gender=='Female');

X2 = Weight(Gender=='Male');
Y2 = Height(Gender=='Male');
%% 
% |X1| and |Y1| are 53-by-1 numeric arrays containing data from the female  
% patients. 
% 
% |X2| and |Y2| are 47-by-1 numeric arrays containing data from the male  
% patients.  
% 
% Create a scatter plot of height vs. weight. Indicate data from the female  
% patients with a circle and data from the male patients with a cross. 

figure
h1 = scatter(X1,Y1,'o');
hold on
h2 = scatter(X2,Y2,'x');

title('Height vs. Weight')
xlabel('Weight (lbs)')
ylabel('Height (in)')