MODIFY LEGEND IN LINEAR REGRESSION

27 visualizaciones (últimos 30 días)
MARCO PIGNATIELLO
MARCO PIGNATIELLO el 7 de En. de 2021
Respondida: Shiva Kalyan Diwakaruni el 13 de En. de 2021
Hi, I have a plot of a linear regression model and its legend set up automatically without specifying any legend function.
I inserted the legend function to modify "data1" in "error bar" but now I dont want to see the fourth field in the legend that MatLab adds automatcally (Confidence bounds2)
I attach the code
clear all
close all
T=readmatrix('Test1_20201201.xls') %importo file excel come se fosse una matrice
Table=importdata('Test1_20201201.xls')%importo file excel come se fosse tabella
ID=Table.textdata(1,2:end)%creo una tabella con solo gli ID dei capisaldi
for a=2:3
Data=T(:,1)
y=T(:,a)
IDs=ID(a-1)
%regressione lineare
reg=fitlm(Data,y)
A=table2array(reg.Coefficients)
Retta=A(:,1)
R2=reg.Rsquared.Ordinary
RMSE=reg.RMSE
format shortG
Tabella=[Retta;R2;RMSE]
% Create Figure with Postion, Title and resize off
f = figure('Name','Data Plot', 'NumberTitle','on', ...
'Position',[Fig_left Fig_bott Fig_width Fig_height], 'Resize','off');
% Create Table on the left side of the figure
uit = uitable(f,'ColumnWidth', 'auto','Data',Tabella,'ColumnName', {'Estimate'},'Rowname',{'Intercept','Slope','<HTML>R<sup>2</sup>','RMSE'},'Position',[1 1 Fig_height (Fig_width/2)]);
uit.FontSize = 8
%uit.Position = [100 140 176 94]
uit.Position = [32.5 148.25 286 94.75]
%residual of adjustment
err=[-0.0013 0.0005 -0.0015 0.0008 0.0011 0.0002]
% Create Plot on the right side of the figure
s1 = subplot(1,2,2)
plot(reg)
hold on; %mantiene plot reg e aggiunge sopra plot errorbar
errorbar(Data,y,err,'.','color','b');
datetick('x','mm/yy')
title(IDs)
ytickformat('%.3f')%decimali
ax = gca;
ax.YRuler.Exponent = 0;%togliere esponente
%2 cm ampiezza asse y
YLIM=ylim
Delta=abs(round(YLIM(1,1),3)-round(YLIM(1,2),3))
Deltay=0.02-Delta
Delta2=(Deltay/2)
ylim([round((YLIM(1,1)-Delta2),3) round((YLIM(1,2)+Delta2),3)])
%tick ogni 2 mm
set(gca,'YTick',[(round((YLIM(1,1)-Delta2),3)) : 0.002 : (round((YLIM(1,2)+Delta2),3))])
ylabel('Q (m)')
xlabel('Date')
%asse x ruotato di 45 gradi
xtickangle(45)
legend('Data','Fit','Confidence bounds','Confidence bounds2', 'Error bar')
%salvataggio file
filename=['C:\Users\Utente\Desktop\TESTmatlab\GRAFICI_TS_REMEDI_barre\' ID{a-1} '.png']
saveas (gcf,filename)
end
Can sameone help me?

Respuestas (1)

Shiva Kalyan Diwakaruni
Shiva Kalyan Diwakaruni el 13 de En. de 2021
Hi,
Instead of inserting of legend function to modify data1" in "error bar" , you can specify the legend labels using the DisplayName property. Set the DisplayName property as a name-value pair when calling the plotting functions
For instance in this situation
instead of
errorbar(Data,y,err','.','color','b');
you can set the DisplayName property inside errorbar like below
errorbar(Data,y,err','.','color','b','DisplayName','Error bar');
and can remove the legend function you have added at the end.
Hence you would be able to eliminate fourth field (Confidence bounds2) and modify the display name
for reference you can check out the below link
hope it helps,
thanks

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by