我正在进行一些线性建模并绘制输出。我希望在标题 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
。
title('text ^{superscript} ', 'Interpreter', 'Tex')
从 documentation 来看,
Tex
是正态图的默认值,大概这是由线性回归模型的 plot
函数更改的,但无论如何,您都可以按照上面的方法重写它。