필터 지우기
필터 지우기

Legend ommit first plot title in multiple plots

조회 수: 1 (최근 30일)
Cesar Adolfo Cruz Vargaya
Cesar Adolfo Cruz Vargaya 2023년 5월 20일
답변: Voss 2023년 8월 23일
I am attempting to create a comparison plot between the original fuzzified data and a fitted version. However, the label for the first plot ('Data #1') is missing from the legend. I have tried adjusting the position of the 'hold on' command, but have been unable to resolve the issue."
clf, figure('Position',[1 1 600 300],'Units','pixels');
hold on
for i=1:length(uCenters)
fo = fitoptions( ...
'Method','NonlinearLeastSquares', ...
'StartPoint',[1 uCenters(i)], ...
'Lower',[0 -Inf]);
ft = fittype('gaussmf(x,[a,c])','options',fo);
f = fit(uSrt,uFCM(i,:)',ft);
H1 = plot(uSrt',uFCM(i,:),'--g');
H1.DisplayName = sprintf('Data #%d',i);
H2 = plot(f,'b');
H2.DisplayName = sprintf('Fuzzy #%d',i);
end
hold off, grid on, legend('Location','eastoutside','Box','off')
xlim([min(uSrt) max(uSrt)])
ylim([0 1.1])
xlabel('Voltaje (V)'), ylabel('Nivel de pertenencia')
title('Voltaje fuzzificado ajustado a curva gaussiana')
  댓글 수: 1
dpb
dpb 2023년 5월 20일
Without data to check against, it's hard to debug exactly, but I'd venture that the first data set are NaN.
Your code also overwirtes the plot and fit handles every pass through the loop so you can't inspect the results of each loop independently after the loop completes; only by setting breakpoint and using the debugger as is. It would be better to save an array of handles, then you could see each result more simply...

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

채택된 답변

Voss
Voss 2023년 8월 23일
First, reproducing the problem using random data:
figure('Position',[1 1 600 300],'Units','pixels')
hold on
for i=1:5
x = rand(10,1);
y = rand(10,1);
fo = fitoptions( ...
'Method','NonlinearLeastSquares', ...
'StartPoint',[1 i], ...
'Lower',[0 -Inf]);
ft = fittype('gaussmf(x,[a,c])','options',fo);
f = fit(x,y,ft);
H1 = plot(x,y,'--g');
H1.DisplayName = sprintf('Data #%d',i);
H2 = plot(f,'b');
H2.DisplayName = sprintf('Fuzzy #%d',i);
end
hold off, grid on, legend('Location','eastoutside','Box','off')
xlabel('Voltaje (V)'), ylabel('Nivel de pertenencia')
title('Voltaje fuzzificado ajustado a curva gaussiana')
You can see that 'Data #1' is missing from the legend, as in your case. I'm not sure what causes that, but storing the line handles - as dpb suggested - and specifying them explicitly in the legend call seems to fix it:
figure('Position',[1 1 600 300],'Units','pixels')
hold on
for i=1:5
x = rand(10,1);
y = rand(10,1);
fo = fitoptions( ...
'Method','NonlinearLeastSquares', ...
'StartPoint',[1 i], ...
'Lower',[0 -Inf]);
ft = fittype('gaussmf(x,[a,c])','options',fo);
f = fit(x,y,ft);
H1(i) = plot(x,y,'--g');
H1(i).DisplayName = sprintf('Data #%d',i);
H2(i) = plot(f,'b');
H2(i).DisplayName = sprintf('Fuzzy #%d',i);
end
hold off, grid on, legend(reshape([H1; H2],[],1),'Location','eastoutside','Box','off')
xlabel('Voltaje (V)'), ylabel('Nivel de pertenencia')
title('Voltaje fuzzificado ajustado a curva gaussiana')

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by