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 ?
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1284715/image.png)
2 Kommentare
the cyclist
am 4 Feb. 2023
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.
Antworten (2)
Voss
am 4 Feb. 2023
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.
0 Kommentare
the cyclist
am 5 Feb. 2023
Bearbeitet: the cyclist
am 5 Feb. 2023
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 Kommentare
the cyclist
am 5 Feb. 2023
Bearbeitet: the cyclist
am 5 Feb. 2023
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.
Siehe auch
Kategorien
Mehr zu Fit Postprocessing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!