# 对分类数据绘图

### 加载样本数据

```load patients whos```
``` Name Size Bytes Class Attributes Age 100x1 800 double Diastolic 100x1 800 double Gender 100x1 11412 cell Height 100x1 800 double LastName 100x1 11616 cell Location 100x1 14208 cell SelfAssessedHealthStatus 100x1 11540 cell Smoker 100x1 100 logical Systolic 100x1 800 double Weight 100x1 800 double ```

### 基于字符向量元胞数组创建分类数组

`Location = categorical(Location);`

`summary(Location)`
``` County General Hospital 39 St. Mary's Medical Center 24 VA Hospital 37 ```

39 位患者在 County General Hospital 就医，24 位患者在 St. Mary's Medical Center 就医，37 位患者在 VA Hospital 就医。

`SelfAssessedHealthStatus` 转换为一个有序分类数组，这样这些类别采用数学排序 `Poor < Fair < Good < Excellent`

```SelfAssessedHealthStatus = categorical(SelfAssessedHealthStatus,... {'Poor' 'Fair' 'Good' 'Excellent'},'Ordinal',true);```

`summary(SelfAssessedHealthStatus)`
``` Poor 11 Fair 15 Good 40 Excellent 34 ```

### 绘制直方图

```figure histogram(SelfAssessedHealthStatus) title('Self Assessed Health Status From 100 Patients')```

```figure histogram(Location(SelfAssessedHealthStatus<='Fair')) title('Location of Patients in Fair or Poor Health')```

### 创建饼图

```figure pie(SelfAssessedHealthStatus); title('Self Assessed Health Status From 100 Patients')```

### 创建帕累托图

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

`pareto` 的第一个输入参数必须是向量。如果分类数组为矩阵或多维数组，则在调用 `countcats``pareto` 之前将其重构为向量。

### 创建散点图

`Gender = categorical(Gender);`

`summary(Gender)`
``` Female 53 Male 47 ```

`Gender` 是一个 100×1 的分类数组，包含两个类别 `Female``Male`

```X1 = Weight(Gender=='Female'); Y1 = Height(Gender=='Female'); X2 = Weight(Gender=='Male'); Y2 = Height(Gender=='Male');```

`X1``Y1` 是 53×1 的数值数组，包含女性患者的数据。

`X2``Y2` 是 47×1 的数值数组，包含男性患者的数据。

```figure h1 = scatter(X1,Y1,'o'); hold on h2 = scatter(X2,Y2,'x'); title('Height vs. Weight') xlabel('Weight (lbs)') ylabel('Height (in)')```