【 MATLAB 】legend 的使用简析

目录

legend

在作图命令中(plot)给出图例标签;

legend(label1,...,labelN)

给当前轴添加图例;

legend(target,___)

给特定轴添加图例;

legend(subset,___)

给部分函数添加图例;

legend(___,'Location',lcn)

指定图例的位置(方向)以及显示的列数;

lgd = legend(___)

给图例添加标题;

legend('boxoff')

去除图例边框;

Modify Legend Appearance

修改图例的外观;


MATLAB 中 legend 可以翻译为图例,下面逐一用例子来说明 legend 的用法,并辅以必要的说明。

我只挑选几种使用的来说明。


legend

在作图命令中(plot)给出图例标签;

Plot two lines. Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text. Then, add a legend.

画两条线。 通过将DisplayName属性设置为所需文本,在绘图命令期间指定图例标签。 然后,添加一个图例(legend)。

% Plot two lines. 
% Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text. 
% Then, add a legend.

clear
clc
close all

x = linspace(0,pi);
y1 = cos(x);
plot(x,y1,'DisplayName','cos(x)')

hold on 
y2 = cos(2*x);
plot(x,y2,'DisplayName','cos(2x)')
hold off

legend

对代码做如下改动,将两幅图分开画,然后添加图例的方法:

% Plot two lines. 
% Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text. 
% Then, add a legend.

clear
clc
close all

x = linspace(0,pi);
y1 = cos(x);
subplot(2,1,1);
plot(x,y1,'DisplayName','cos(x)')

legend

%hold on 
y2 = cos(2*x);
subplot(2,1,2);
plot(x,y2,'DisplayName','cos(2x)')
% hold off

legend


legend(label1,...,labelN)

给当前轴添加图例;

legend(label1,...,labelN) sets the legend labels. Specify the labels as a list of character vectors or strings, such as legend('Jan','Feb','Mar').

legend(label1,...,labelN)设置图例标签。 将标签指定为字符向量或字符串列表,例如legend('Jan','Feb'&#x

``` clear; clc; close all; a = importdata('GaCo05_02.txt'); size(a); time = a(:,1); b1 = a(:,2); b2 = a(:,3); b3 = a(:,4); b4 = a(:,7); b5 = a(:,8); b6 = a(:,9); c = b1 + b2 + b3; d = b4 + b5 + b6; for i = 1:1:length(c) if c(i) >= 40 HT(i) = 1; else HT(i) = 0; end if d(i) >= 40 BT(i) = 1; else BT(i) = 0; end gait(i) = HT(i) + 2*BT(i) + 1; end gaitT = gait'; input = [c(1:end,1) d(1:end,1)]; Train_num = 2500; train_data = [c(1:Train_num,1) d(1:Train_num,1)]; train_data = zscore(train_data); train_label = gaitT(1:Train_num,1);%%%正态分布归一化 test_num = 10000; test_data = [c(Train_num+1:test_num,1) d(Train_num+1:test_num,1)]; test_data = zscore(test_data);%%%正态分布归一化 test_label = gaitT(Train_num+1:test_num,1); input_train = train_data; output_train = train_label; input_test = test_data; output_test = test_label; % size(input_train) %% 900 x 9 % size(output_train) %% 900 x 1 % size(input_test) %% 300 x 9 % size(output_test) %% 300 x 1 input_train = input_train'; input_test = input_test'; %归一化 [inputn_train,inputps]=mapminmax(input_train); [inputn_test,inputtestps]=mapminmax('apply',input_test,inputps); num_iters = 40; params.max_depth = 2; params.eta = 0.1; max_sort = 4; %类别数 model1 = xgboost_train(inputn_train', output_train, params, num_iters); % 仿真测试 best_pre = xgboost_test(inputn_test', model1, max_sort); rightratio = sum((best_pre == output_test)) / length(output_test); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% confMat = confusionmat(output_test,best_pre); %output_test是真实值标签 figure; set(gcf,'unit','centimeters','position',[5 2 13 9]) zjyanseplotConfMat(confMat.'); xlabel('Predicted label') ylabel('Real label') title(['Xgboost分类准确率:',num2str(rightratio*100),'%'],'fontsize',12,'fontname','TimesNewRoman'); hold off %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% figure plot(1:length(output_test),output_test,'b') hold on plot(1:length(best_pre),best_pre,'g') legend('标记类型','识别类型') title({'Xgboost的预测效果',['测试集正确率 = ',num2str(rightratio*100),' %']}) xlabel('预测样本编号') ylabel('分类结果') set(gca,'fontsize',12) axis([0 800 1 4.2])```图的是那些量
04-02
<think>好的,我现在需要仔细分析用户提供的MATLAB代码,并理解他想要通过这段代码绘制哪些量。用户的问题是“图的是那些量”,也就是询问代码中生成的图表展示了哪些数据或变量。 首先,我需要通读整个代码,了解其结构和功能。代码大致分为几个部分:数据导入、预处理、逻辑判断生成gait变量、数据划分、归一化处理、XGBoost模型训练与测试、准确率计算以及最后的绘图部分。 接下来,重点关注绘图相关的部分。代码中有两个figure块,分别生成了混淆矩阵和预测效果图。我需要逐一分析这两个图的数据来源和绘制的内容。 第一个figure块: ```matlab confMat = confusionmat(output_test,best_pre); figure; ... zjyanseplotConfMat(confMat.'); xlabel('Predicted label') ylabel('Real label') title(['Xgboost分类准确率:',num2str(rightratio*100),'%'],'fontsize',12,'fontname','TimesNewRoman'); ``` 这里使用confusionmat函数生成混淆矩阵confMat,对比测试集真实标签output_test和预测结果best_pre。然后通过自定义函数zjyanseplotConfMat绘制混淆矩阵的热力图。横轴是预测标签,纵轴是真实标签,标题显示分类准确率。因此,第一个图展示的是分类模型在测试集上的混淆矩阵,反映不同类别的分类情况。 第二个figure块: ```matlab figure plot(1:length(output_test),output_test,'b') hold on plot(1:length(best_pre),best_pre,'g') legend('标记类型','识别类型') title({'Xgboost的预测效果',['测试集正确率 = ',num2str(rightratio*100),' %']}) xlabel('预测样本编号') ylabel('分类结果') ... ``` 这里绘制了两条曲线:蓝色线是真实标签output_test,绿色线是预测结果best_pre。横轴是样本编号,纵轴是分类结果(1到4的类别)。因此,第二个图展示了测试集中每个样本的真实类别与预测类别的对比,直观显示模型预测的效果和错误点。 接下来需要确认变量来源是否正确。例如,output_test是测试集的真实标签,而best_pre是模型预测结果。混淆矩阵的正确生成依赖于这两个变量的正确对应。另外,归一化处理可能影响模型性能,但绘图部分的数据已经是归一化后的处理结果,不过绘图时使用的是原始标签和预测结果,没有涉及特征数据本身。 还需要注意gait变量的生成逻辑。在循环中,根据c和d的值是否超过40生成HT和BT,进而计算出gait值(1到4)。因此,分类结果是四个类别,对应不同的步态情况。混淆矩阵的类别数应为4,预测效果图中的纵轴范围也设置为1到4.2,符合这一点。 最后,检查是否有遗漏的变量或绘图部分。例如,代码中可能还有其他绘图命令,但用户提供的代码中只有这两个figure块。因此,用户的问题应指向这两个图所展示的量。</think>该MATLAB代码生成了两个核心图形,分别用于评估XGBoost分类器的性能。以下是每个图形所展示的量和含义解析: --- ### **图1:混淆矩阵热力图** ```matlab confMat = confusionmat(output_test,best_pre); zjyanseplotConfMat(confMat.'); ``` **展示的量:** 1. **真实标签分布(纵轴)**:来自 `output_test`,表示测试集的真实类别标签,共4类(取值1-4)。 2. **预测标签分布(横轴)**:来自 `best_pre`,表示模型对测试集的预测结果。 3. **混淆矩阵数值**:矩阵元素 $C_{ij}$ 表示真实类别为 $i$ 的样本被预测为类别 $j$ 的数量。 4. **分类准确率**:通过标题显示总体正确率 $rightratio \times 100\%$,计算公式为: $$ \text{正确率} = \frac{\sum_{i=1}^4 C_{ii}}{\sum_{i,j} C_{ij}}} $$ **作用**:评估模型对每个类别的识别能力,观察是否存在类别混淆(如误将类别2预测为类别3)。 --- ### **图2:预测效果对比曲线** ```matlab plot(1:length(output_test),output_test,'b'); % 真实标签(蓝色) plot(1:length(best_pre),best_pre,'g'); % 预测标签(绿色) ``` **展示的量:** 1. **样本编号(横轴)**:测试集样本的索引,范围由 `length(output_test)` 决定(代码中为800)。 2. **分类结果(纵轴)**: - 蓝色曲线:真实标签 `output_test`,取值1-4。 - 绿色曲线:预测标签 `best_pre`,取值与真实标签对应。 3. **正确率标注**:标题显示测试集正确率 $rightratio \times 100\%$。 **作用**:直观对比每个样本的真实类别与预测结果,定位预测错误的样本位置(蓝色与绿色不重合的点)。 --- ### **关键变量说明** | 变量名 | 定义 | 取值范围 | 用途 | |----------------|------------------------------------|------------|--------------------| | `gait` | 步态类别编码(HT + 2*BT + 1) | 1, 2, 3, 4 | 分类标签生成 | | `output_test` | 测试集真实标签 | 1-4 | 混淆矩阵纵轴/蓝线 | | `best_pre` | 模型预测结果 | 1-4 | 混淆矩阵横轴/绿线 | | `c`, `d` | 传感器信号和(归一化前原始特征) | 连续值 | 模型输入特征 | --- ### **代码逻辑简析** 1. **数据预处理**:通过阈值判断生成离散类别标签 `gait`,划分训练集与测试集,并进行Z-score归一化。 2. **模型训练**:使用XGBoost对归一化后的 `train_data` 训练,参数包括树深度 `max_depth=2` 和学习率 `eta=0.1`。 3. **性能评估**:通过混淆矩阵和预测曲线可视化模型在测试集上的表现。 若需进一步优化模型或调整图形样式,可修改XGBoost参数(如 `max_depth`)或调整绘图函数中的颜色、坐标轴范围等属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李锐博恩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值