필터 지우기
필터 지우기

Creating legend from fittype function

조회 수: 3 (최근 30일)
Ran Kagan
Ran Kagan 2022년 5월 30일
댓글: Ran Kagan 2022년 5월 30일
Hi,
I've been trying to create legends for numerous curves I plotted (using fittype/fit functions) on the same graph, but with no success.
All "legend" functions didn't seem to work like I wanted. See my code below with my "%"s as my trial and erros.
Would apprecaite your help in this matter!
Thanks.
x_alpha = [0,2,5];
SF_alpha=[1, 0.0505, 0.02971];
SF_alpha_TMZ=[0.4347, 0.03565, 0.01657];
x_gamma = [0,2,5];
SF_gamma=[1,0.43571,0.07857];
SF_gamma_TMZ=[0.67142,0.35428,0.04642];
% Alpha
g=fittype('exp1');
myfit=fit(x_alpha',SF_alpha',g)
hL1=plot(myfit, x_alpha, SF_alpha)
xlim([0 5.5])
hold on
myfit1=fit(x_alpha',SF_alpha_TMZ',g)
hL2=plot(myfit1, x_alpha, SF_alpha_TMZ)
% Gamma
m=fittype('exp(-a*x-b*x^2)');
myfit2=fit(x_gamma',SF_gamma',m)
hL3=plot(myfit2, x_gamma, SF_gamma)
myfit3=fit(x_gamma',SF_gamma_TMZ',m)
hL4=plot(myfit3, x_gamma, SF_gamma_TMZ)
hold off
set([hL1(2)],'color','k')
set([hL2(2)] ,'color','b')
set([hL3(2)] ,'color','m')
set([hL4(2)] ,'color','r')
%h=[hL1;hL2;hL3;hL4];
%legend(h,'Alpha SF','Alpha SF+TMZ','Gamma SF','Gamma SF+TMZ');
%legend([hL1;hL2;hL3;hL4], 'Alpha SF','Alpha SF+TMZ','Gamma SF','Gamma SF+TMZ')
%legend(h, {'Alpha SF','Alpha SF+TMZ','Gamma SF','Gamma SF+TMZ'}, 'Location', 'BestOutside')
set(gca, 'YScale', 'log')

답변 (1개)

KSSV
KSSV 2022년 5월 30일
Instead of this line:
hL1=plot(myfit, x_alpha, SF_alpha) ;
use:
xi = linspace(min(x_alpha),max(x_alpha)) ;
hL1=plot(xi,myfit(xi), x_alpha, SF_alpha)
And use legend.
  댓글 수: 1
Ran Kagan
Ran Kagan 2022년 5월 30일
Hi,
I tried using your suggested lines but it draws linear lines rather than using the "exp1" and the second exponent function I wanted.
I suppose there's a method to create the legend simply by using the legend syntax correctly without changing the code itself?

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by