# 对分类数据绘图

### 加载样本数据

```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` 转换为一个有序 `categorical` 数组，这样这些类别采用数学排序 `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")```

### 创建饼图

`categorical` 数组直接创建饼图。

```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` 的第一个输入参量必须是向量。如果 `categorical` 数组为矩阵或多维数组，则在调用 `countcats``pareto` 之前将其重构为向量。

### 创建散点图

```X1 = Diastolic(SelfAssessedHealthStatus <= "Fair"); Y1 = Systolic(SelfAssessedHealthStatus <= "Fair"); X2 = Diastolic(SelfAssessedHealthStatus >= "Good"); Y2 = Systolic(SelfAssessedHealthStatus >= "Good");```

`X1``Y1` 是 26×1 数值数组，包含健康状况为 `Poor``Fair` 的患者的数据。

`X2``Y2` 是 74×1 数值数组，包含健康状况为 `Good``Excellent` 的患者的数据。

```figure h1 = scatter(X1,Y1,"o"); hold on h2 = scatter(X2,Y2,"x"); title("Blood Pressure for Groups of Patients Assessing Self Health"); xlabel("Diastolic (mm Hg)") ylabel("Systolic (mm Hg)") legend("Poor or Fair","Good or Excellent")```