How to put a label on each plotted curve

43 ビュー (過去 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 Center および File ExchangeFit Postprocessing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by