我正在进行一些线性建模并绘制输出。我希望在标题 title doc中包含 r^2 值,但以 2 作为上标。
由于某种原因,在绘制线性模型输出fitlm doc时,文档中的代码行未按预期生成标题。如何解决这个问题?
clear
clc
%Make test data
%----------------------------
x1 = 1:0.01:3;
r = -1 + (1+1)*rand(1,201);
x1 = x1 + r;
y1 = 1:0.01:3;
r = -1 + (1+1)*rand(1,201);
y1 = y1 + r;
x1 = x1';
y1 = y1';
label1 = zeros(length(y1),1);
Tclust1 = [label1,x1,y1];
%----------------------------
x2 = 4:0.01:6;
r = -1 + (1+1)*rand(1,201);
x2 = x2 + r;
y2 = 10:0.01:12;
r = -1 + (1+1)*rand(1,201);
y2 = y2 + r;
x2 = x2';
y2 = y2';
label2 = ones(length(y2),1);
Tclust2 = [label2,x2,y2];
%----------------------------
x3 = 5:0.01:7;
r = -1 + (1+1)*rand(1,201);
x3 = x3 + r;
y3 = 11:0.01:13;
r = -1 + (1+1)*rand(1,201);
y3 = y3 + r;
x3 = x3';
y3 = y3';
label3 = label2+1;
Tclust3 = [label3,x3,y3];
%----------------------------
T = [Tclust1;Tclust2;Tclust3];
%Prep for LM -------------------------------------------------------------
%Classify by labels
Label_idx = zeros(length(T(:,1)),1);
for qq = 1:length(Label_idx)
if T(qq,1) > 0
Label_idx(qq) = 1;
end
end
for qq = 1:length(Label_idx)
if T(qq,1) > 1
Label_idx(qq) = 2;
end
end
%dummy variables
for qq = 1:length(Label_idx)
if Label_idx(qq) == 0
Label_1(qq) = 1;
else
Label_1(qq) = 0;
end
end
Label_1 = Label_1';
for qq = 1:length(Label_idx)
if Label_idx(qq) == 2
Label_2(qq) = 1;
else
Label_2(qq) = 0;
end
end
Label_2 = Label_2';
%Do the Linear modelling -------------------------------------------------
%Input predictor
pred = (T(:,2));
%Input comparator
comp = (T(:,3));
%interaction parameters
int_lo = pred.*Label_1;
int_hi = pred.*Label_2;
%predictors
X = [pred,Label_1,Label_2,int_lo,int_hi];
LR_mdl = fitlm(X,comp);
%plot ==========================================================
plot(LR_mdl)
arrayfun( @(x) set(x,'DisplayName',strrep(x.DisplayName,'*',' × ')), findall(gcf,'type','line') )
% title('title^2')
% title('^{super} normal _{sub}')
title('text ^{superscript} ')
figure(2)
scatter(x1,y1)
title('text ^{superscript} ')
`
请注意,当仅绘制部分原始数据(而非线性模型)时,代码行确实会为图 2 生成所需的上标。
您需要更改
Interpreter
标题。从文档来看,
Tex
是正态图的默认值,大概这是由plot
线性回归模型的函数改变的,但无论如何您都可以按照上述方式覆盖它。