How to put a label on each plotted curve

조회 수: 46 (최근 30일)
Manar Anwar
Manar Anwar 2022년 6월 22일
답변: Abhishek Tiwari 2022년 6월 26일
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

채택된 답변

Abhishek Tiwari
Abhishek Tiwari 2022년 6월 26일
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:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by