How to put a label on each plotted curve

51 views (last 30 days)
Manar Anwar
Manar Anwar on 22 Jun 2022
Answered: Abhishek Tiwari on 26 Jun 2022
Having the following code that would plot two curves on the same plot, the plot fr theoriginal data and the plot for the fitted curve that is being fitted using the function createFit. I have computed the area under the curve for both curves and i would like to put it as a label on each curve in the plot how can i possibly do that?
This is the code I am using
TestData.Date2 = datetime(string(TestData.Date), "InputFormat", "uuuuMMddHHmm");
dates = unique(TestData.Date2);
for ii = 1:20
tDate = TestData(TestData.Date2 == dates(ii), :);
Int = trapz(tDate.GDALT, tDate.NE8);
Figure=figure(ii);
plot(tDate.GDALT, tDate.NE8);
hold on
%plot( tDate.predections, tDate.GDALT, 'red');
[xData, yData] = prepareCurveData( tDate.GDALT, tDate.NE8 );
% Set up fittype and options.
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 0.00121653578719157;
% Fit model to data.
[fitresult, gof] = fit( tDate.GDALT,tDate.NE8, ft, opts );
% Plot fit with data.
%figure( 'Name', 'untitled fit 1' );
Int2 = trapz(xData, yData);
h = plot( fitresult, xData, yData );
Diff=Int-Int2;
xlabel("NE8");
ylabel("GDALT");
title(string(dates(ii)));
legend({'Original','Constructed'})
end

Answers (1)

Abhishek Tiwari
Abhishek Tiwari on 26 Jun 2022
Hi,
One method is to choose any point on each curve and add text description to it that may serve as a label. Similarly, LineMarks can also be utilized to add text on curve.
Example:
x = -pi:0.01:pi;
y1 = cos(x);
y2 = sin(x);
plot(x,y1,'b-'); hold on
plot(x,y2,'r-'); hold off
text(x(end),y1(end),"cos(x)",'Color','blue');
text(x(end),y2(end),"sin(x)",'Color','red');
These might be useful:

Community Treasure Hunt

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

Start Hunting!