i don't know why the legend doesn't take the same type of line that simulated , for example if dashed line used , so the legend should be dashed also ?

조회 수: 18 (최근 30일)
  댓글 수: 2
the cyclist
the cyclist 2023년 2월 4일
We don't know why, either.
If you share the code that creates the figure, then maybe we can help you figure it out. You can use the paper clip icon in the INSERT section of the toolbar.
It's best to share the smallest possible piece of code that will allow us to replicate the problem.
Hagar Hendy
Hagar Hendy 2023년 2월 5일
이동: the cyclist 2023년 2월 5일
here is the code
% with large scale of noise = 10 fastfast
xData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
yData = [100 100 100 100 100 100 100 100 88 74 48 22 7 0 0 0 0 0 0 0 0]'
% Set up fittype and options.
ft1 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts1 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts1.Display = 'Off';
opts1.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult1, gof1] = fit( xData, yData, ft1, opts1 );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h1=plot( fitresult1, xData, yData,'-');
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% with large scale of noise = 5 (fastslow)
tData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
bData = [100 100 100 100 100 100 100 96 87 75 47 20 9 4 0 0 0 0 0 0 0]';
% Set up fittype and options.
ft2 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts2 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts2.Display = 'Off';
opts2.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult2, gof2] = fit( tData, bData, ft2, opts2 );
% Plot fit with data.
hold on
h2 =plot( fitresult2, tData, bData,'--');
%legend('Moderate Noise', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%with large scale of noise = 1 (tt)
%sData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
sData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
eData = [100 100 100 100 100 100 99 93 86 71 49 21 7 1 0 0 0 0 0 0 0]';
%eData = [100 100 100 100 100 100 100 100 100 100 46 0 0 0 0 0 0 0 0 0 0]';
ft3 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts3 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts3.Display = 'Off';
opts3.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult3, gof3] = fit( sData, eData, ft3, opts3 );
% Plot fit with data.
hold on
h3= plot( fitresult3, sData, eData,'o');
%%%%%%%%%%%%%%%%%%%s
% with large scale of noise = 10 (ss)
kData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
LData = [100 100 100 100 100 100 100 99 85 65 48 25 8 3 0 0 0 0 0 0 0]';
%eData = [100 100 100 100 100 100 100 100 100 100 46 0 0 0 0 0 0 0 0 0 0]';
ft4 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts4 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts4.Display = 'Off';
opts4.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult4, gof4] = fit( kData, LData, ft4, opts4 );
% Plot fit with data.
hold on
h4= plot( fitresult4, kData, LData);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%slowfast
oData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
uData = [100 100 100 100 100 100 100 93 87 73 46 26 9 0 0 0 0 0 0 0 0]';
%eData = [100 100 100 100 100 100 100 100 100 100 46 0 0 0 0 0 0 0 0 0 0]';
ft6 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts6 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts6.Display = 'Off';
opts6.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult6, gof6] = fit( oData, uData, ft6, opts6 );
% Plot fit with data.
hold on
h5= plot( fitresult6, oData, uData,'-.');
set(h1,'color','k', 'linewidth',2)
set(h2,'color','c','linewidth',2)
set(h3,'color','r', 'linewidth',2)
set(h4,'color','b', 'linewidth',2)
set(h5,'color','g', 'linewidth',2)
% Label axes
xlabel('Time Difference (ps)', 'fontsize',12);
ylabel('Probability','fontsize',12);
grid off
legend('', 'FF','','FS','','TT','','SS','','SF', 'Location', 'NorthEast', 'Interpreter', 'none' );

댓글을 달려면 로그인하십시오.

답변 (2개)

Voss
Voss 2023년 2월 4일
This happens when you specify fewer legend entries than you have lines in your axes. For example:
% plot three lines:
plot(1:10,'-b')
hold on
plot(2:11,'--r')
plot(3:12,':m')
% make a legend for the first two lines only:
legend({'red' 'blue'})
It's also possible that some of your plot calls create more than one line, as in:
figure
plot(magic(3)) % this creates three lines
hold on
plot(1:10,'g') % this creates one line
% now make a legend with two entries, thinking you created two lines
% because you called plot() two times. In fact you created four lines,
% so the 3rd and 4th lines don't show up in the legend.
legend('line 1','line 2')
If neither of those two situations seems to apply, then share your code so people can try to figure it out.

the cyclist
the cyclist 2023년 2월 5일
편집: the cyclist 2023년 2월 5일
The problem is what @Voss described. Here, I've removed your legend completely. When you use the plot() function with a cfit object, it automatically creates a legend. Notice that you actually have 10 plotted objects.
% with large scale of noise = 10 fastfast
xData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
yData = [100 100 100 100 100 100 100 100 88 74 48 22 7 0 0 0 0 0 0 0 0]';
% Set up fittype and options.
ft1 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts1 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts1.Display = 'Off';
opts1.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult1, gof1] = fit( xData, yData, ft1, opts1 );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h1=plot( fitresult1, xData, yData,'-');
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% with large scale of noise = 5 (fastslow)
tData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
bData = [100 100 100 100 100 100 100 96 87 75 47 20 9 4 0 0 0 0 0 0 0]';
% Set up fittype and options.
ft2 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts2 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts2.Display = 'Off';
opts2.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult2, gof2] = fit( tData, bData, ft2, opts2 );
% Plot fit with data.
hold on
h2 =plot( fitresult2, tData, bData,'--');
%legend('Moderate Noise', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%with large scale of noise = 1 (tt)
%sData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
sData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
eData = [100 100 100 100 100 100 99 93 86 71 49 21 7 1 0 0 0 0 0 0 0]';
%eData = [100 100 100 100 100 100 100 100 100 100 46 0 0 0 0 0 0 0 0 0 0]';
ft3 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts3 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts3.Display = 'Off';
opts3.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult3, gof3] = fit( sData, eData, ft3, opts3 );
% Plot fit with data.
hold on
h3= plot( fitresult3, sData, eData,'o');
%%%%%%%%%%%%%%%%%%%s
% with large scale of noise = 10 (ss)
kData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
LData = [100 100 100 100 100 100 100 99 85 65 48 25 8 3 0 0 0 0 0 0 0]';
%eData = [100 100 100 100 100 100 100 100 100 100 46 0 0 0 0 0 0 0 0 0 0]';
ft4 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts4 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts4.Display = 'Off';
opts4.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult4, gof4] = fit( kData, LData, ft4, opts4 );
% Plot fit with data.
hold on
h4= plot( fitresult4, kData, LData);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%slowfast
oData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
uData = [100 100 100 100 100 100 100 93 87 73 46 26 9 0 0 0 0 0 0 0 0]';
%eData = [100 100 100 100 100 100 100 100 100 100 46 0 0 0 0 0 0 0 0 0 0]';
ft6 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts6 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts6.Display = 'Off';
opts6.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult6, gof6] = fit( oData, uData, ft6, opts6 );
% Plot fit with data.
hold on
h5= plot( fitresult6, oData, uData,'-.');
set(h1,'color','k', 'linewidth',2)
set(h2,'color','c','linewidth',2)
set(h3,'color','r', 'linewidth',2)
set(h4,'color','b', 'linewidth',2)
set(h5,'color','g', 'linewidth',2)
% Label axes
xlabel('Time Difference (ps)', 'fontsize',12);
ylabel('Probability','fontsize',12);
grid off
legend([h1; h2; h3; h4; h5],{'', 'FF','','FS','','TT','','SS','','SF'}, 'Location', 'NorthEast', 'Interpreter', 'none' );
  댓글 수: 3
the cyclist
the cyclist 2023년 2월 5일
편집: the cyclist 2023년 2월 5일
Ah, I understand what you were trying to do now. I edited the code above to explicitly include the plot handles
[h1; h2; h3; h4; h5]
to the legend function. Each individual handle (e.g. h1) is the handle to two plotted objects. You could instead do something like
legend([h1(2); h2(2); h3(2); h4(2); h5(2)],{'FF','FS','TT','SS','SF'}, 'Location', 'NorthEast', 'Interpreter', 'none' );
to label only the fits, which seems like what you want to do.
Hagar Hendy
Hagar Hendy 2023년 2월 5일
편집: Hagar Hendy 2023년 2월 5일
thank you so much for your help @the cyclist and @Voss for great help and support, and sorry for late reply

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by