2013-04-01 38 views
0

我在Matlab中繪製圖片如下。我已經嘗試了很多方法在圖片的最後放置漂亮的彩色圖例,每種顏色代表一個變量,例如'通貨膨脹','利率','匯率'等問題是我無法正確添加它們。我的代碼如下圖所示。如何在Matlab中將顏色圖例置入圖中?

我使用命令

fill 

繪製的曲線圖,數據是矩陣(在下面的代碼沒有提供,過大)。任何人都會教我如何在圖表底部繪製彩色圖例嗎?謝謝!

enter image description here

%% Graph 1 
z1 = squeeze(z(i_var(1),:,:)); 
xmin = x(1); 
xmax = x(end); 
ix = z1 > 0; 
ymax = max(sum(z1.*ix)); 
ix = z1 < 0; 
ymin = min(sum(z1.*ix)); 
if ymax-ymin < 1e-6 
end 

    figure('Name',endo_names(i_var(1),:)); 
subplot(2,1,1) 
plot(x(2:end),z1(end,:),'k-','LineWidth',2) 
hold on; 
for i=1:gend 
    i_1 = i-1; 
    yp = 0; 
    ym = 0; 
    for k = 1:comp_nbr 
     zz = z1(k,i); 
     if zz > 0 
      fill([x(i) x(i) x(i+1) x(i+1)],[yp yp+zz yp+zz yp],k); 
      yp = yp+zz; 
     else 
      fill([x(i) x(i) x(i+1) x(i+1)],[ym ym+zz ym+zz ym],k); 
      ym = ym+zz; 
     end 
     hold on; 
    end 
end 
plot(x(2:end),z1(end,:),'k-','LineWidth',2), 
    set(gca,'xtick',[0 22 44 66 88 110]), 
    set(gca,'xticklabel',{'1985q1', '1990q3', '1996q1', '2001q3', '2007q3', '2013q1'}),title('Output gap') 

axis([0 110 -3 3]) 
hold off; 






%% Graph 2 
z1 = squeeze(z(i_var(2),:,:)); 
xmin = x(1); 
xmax = x(end); 
ix = z1 > 0; 
ymax = max(sum(z1.*ix)); 
ix = z1 < 0; 
ymin = min(sum(z1.*ix)); 
if ymax-ymin < 1e-6 
end 

subplot(2,1,2) 
plot(x(2:end),z1(end,:),'k-','LineWidth',2) 
hold on; 
for i=1:gend 
    i_1 = i-1; 
    yp = 0; 
    ym = 0; 
    for k = 1:comp_nbr 
     zz = z1(k,i); 
     if zz > 0 
      fill([x(i) x(i) x(i+1) x(i+1)],[yp yp+zz yp+zz yp],k); 
      yp = yp+zz; 
     else 
      fill([x(i) x(i) x(i+1) x(i+1)],[ym ym+zz ym+zz ym],k); 
      ym = ym+zz; 
     end 
     hold on; 
    end 
end 
plot(x(2:end),z1(end,:),'k-','LineWidth',2),set(gca,'xtick',[0 22 44 66 88 110]) 
set(gca,'xticklabel',{'1985q1', '1990q3', '1996q1', '2001q3', '2007q3', '2013q1'}), title('CPI inflation') 

axis([0 110 -3 3]) 
hold off; 

回答

1

我不知道fill有辦法使用傳統legend,所以這裏就是我想嘗試:每幅圖後創建虛擬的情節和使用它們的顏色是一樣的您fill對象,然後使用常規的傳說:

hold on 
L1 = plot(NaN,NaN,'r',NaN,NaN,'b',NaN,NaN,'y'); 
legend(L1,'inflation', 'interest rate' ,'exchange rate'); 

設置的傳奇位置看到documentation,例如:

legend(L1,'inflation', 'interest rate' ,'exchange rate','Location','SouthOutside','Orientation','horizontal'); 
0

我會使用命令:colorbar,爲您的目的。 該命令允許使用某些輸入參數來選擇以足夠的單詞表示哪種顏色。嘗試一下。

相關問題