2014-11-12 3 views
0

Matlab에서 그래프에 범례를 추가하려고합니다.Matlab에 범례 추가

각 색상은 다른 범례를 가져야합니다. 즉, 빨간색 'r'은 상향 조절되어야하고 파란색 'b'는 하향 조절되고 녹색 'g'조절되지 않아야합니다.

색상을 설정 한 직후 코드를 넣으려고했으나 제대로 작동하지 않습니다.

아이디어가 있으십니까?

function functionForNumber5(M) 

fig = figure; 
ax = axes('nextplot','add','xlim',[1,5],'ylim',[0,2],'xtick',   
[1:.5:5],'ytick',[0:.5:2]); 
xlabel('Time (Hours)'); 
ylabel('Expression Level'); 
    for it1 = 1 : 24 
    if M(5,it1) > 1.3 
     currentColor = 'r'; 
    elseif M(5,it1) < 0.7 
     currentColor = 'b'; 
    else 
     currentColor = 'g'; 
    end 
    plot(ax,[1:5],M(:,it1),'color',currentColor); 
end 

은 정말 3 키 전설 만에, 즉, 내 플롯이 하향 조절 또는 nonregulated, 상향 조절 여부를 나타내는 컬러 키가 필요합니다.

+0

편집 한 질문에 따라 답변을 편집했습니다. – am304

답변

1

, 당신은 세 가지 색상 중 하나 그래프에 24 개 라인을 가지고, 당신은 전설이 해당 텍스트 만 3 색을 보여주고 싶어요. 코드가 각 색상의 적어도 하나의 음모가 있다고 가정

function functionForNumber5(M) 

fig = figure; 
ax = axes('nextplot','add','xlim',[1,5],'ylim',[0,2],'xtick',[1:.5:5],'ytick',[0:.5:2]); 
xlabel('Time (Hours)'); 
ylabel('Expression Level'); 
h_plot = zeros(1,24); % vector of plot handles 
col_idx = zeros(1,3); % vector of indices corresponding to the 3 colours 
for it1 = 1 : 24 
    if M(5,it1) > 1.3 
     currentColor = 'r'; 
     col_idx(1) = it1; 
    elseif M(5,it1) < 0.7 
     currentColor = 'b'; 
     col_idx(2) = it1; 
    else 
     currentColor = 'g'; 
     col_idx(3) = it1; 
    end 
    h_plot(it1) = plot(ax,[1:5],M(:,it1),'color',currentColor); 
end 
legend(h_plot(col_idx),'upregulated','downregulated','unregulated') 

하는 것으로 col_idx의 해당 값이 0 및되기 때문에 사용하는 경우, 그렇지 않으면 오류가 밖으로 것 : 여기에 내가 일을해야한다고 생각 해결책은이다 h_plot으로 색인을 생성하면 작동하지 않습니다.

0

matlab에 범례 기능이 있습니다.

x = -pi:pi/20:pi; 
y1 = sin(x); 
y2 = cos(x); 

figure 
plot(x,y1,'-ro',x,y2,'-.b') 
legend('sin(x)','cos(x)') 

이렇게하면 y1과 y2에 각각 2 개의 항목이있는 범례가 작성됩니다.

참조 : 내가 제대로 질문을 이해한다면 http://es.mathworks.com/help/matlab/ref/legend.html#