2014-10-01 563 views
0

我無法將我的圖形與我的座標軸相匹配。前兩個小區工作,後兩個小區不工作。我試圖繪製兩個Argo浮標的溫度與壓力,然後繪製鹽度與壓力的關係曲線。這裏是我的代碼:Matlab - 圖例與我的圖形不匹配

% First Plot 
subplot(221); 
plot(float1winter.T,float1winter.P,'b');  
hold on; 
plot(float1summer.T,float1summer.P,'r'); 
hold on; 
tempAdiff = abs(float1summer.T-float1winter.T) 
plot(tempAdiff,float1summer.P,'--k'); 
hold on; 
set(gca,'ydir','reverse'); 
title('Argo Float #1901440 Temp Profiles'); 
legend(['float1winter','float1summer','tempAdiff'],{'11-29-2013','07-01-2013','Temperature Difference'},'location','southwest'); 
xlabel('Temperature (°C)'); 
ylabel('Pressure'); 
shg; 



% Second Plot 
subplot(222); 
plot(float2winter.S,float2winter.P,'m'); 
hold on; 
plot(float2summer.S,float2summer.P,'c'); 
hold on; 
set(gca,'ydir','reverse'); 
title('Argo Float #1901440 Salinity Profiles'); 
legend(['float2winter','float2summer'],{'11-29-2013','06-02-2013'},'location','southwest'); 
xlabel('Salinity (psu)'); 
ylabel('Presure'); 
shg; 



% Third Plot 
subplot(223); 
% Matrix demensions did not agree bewteen winter and summer profiles. The summer profile was 71 x 2 and the winter was 70 x 2. I tried "reshape" 
% and that didn't work. So I changed the vector of float3summer.T to 
% float3bsummer.T with an array of 70 x 2 
float3bsummer.T = float3summer.T(1:70,1:2); 
float3bsummer.P = float3summer.P(1:70,1:2); 
plot(float3winter.T,float3winter.P,'Linewidth',1,'color','blue'); 
hold on; 
plot(float3bsummer.T,float3bsummer.P,'Linewidth',1,'color','red'); 
hold on; 
tempdiff = abs(float3bsummer.T-float3winter.T) 
plot(tempdiff,float3bsummer.P,'--k'); 
hold on; 
set(gca,'ydir','reverse'); % this line reverses the y-axis so that depth increases downward 
title('Argo Float #1901415 Tempearture Profiles'); 
hold on; 
summerfloat = plot(float3bsummer.T,float3bsummer.P,'r'); 
legend(['float3winter.T','summerfloat','tempdiff'],{'12-03-2013','07-03-2013','Temp Diff'},'location','southeast'); 
xlabel('Temperature (°C)'); 
ylabel('Pressure'); 
axis ([-1,4,0,2000]); 
shg; 


% Fourth Plot 
subplot(224); 
plot(float3winter.S,float3winter.P,'g'); 
% Changed matrix dimensions for Salinity of Summer 
float3bsummer.S = float3summer.S(1:70,1:2); 
float3bsummer.P = float3summer.P(1:70,1:2); 
plot(float3bsummer.S,float3bsummer.P,'.b'); 
hold on; 
set(gca,'ydir','reverse'); 
title('Argo Float #1901415 Salinity Profiles'); 
h4 = plot(float3winter.S,float3winter.P,'g'); 
hold on; 
h5 = plot(float3bsummer.S,float3bsummer.P,'.b'); 
hold on; 
legend(['float3winter','float3bsummer.T'],{'12-03-2013','07-03-2013'},'location','southwest'); 
xlabel('Salinity (psu)'); 
ylabel('Pressure'); 
axis ([33.8,34.8,0,2000]); 
shg; 



% Save File to Desktop 
set(gcf,'color','w'); 
saveas(gcf,'~/Desktop/hw1_figure1.pdf','pdf');![enter image description here][1] 
+2

什麼不符合?你能向我們展示這些情節的圖像並解釋你期望看到什麼嗎? – 2014-10-01 12:26:29

回答

3

我猜你是要爲你的傳奇,{'11-29-2013','07-01-2013','Temperature Difference'},一組琴絃關聯與變量['float1winter','float1summer','tempAdiff']取得該地塊。

但是,這不是legend的工作原理。 MATLAB沒有辦法將plot(float1winter.T,float1winter.P,'b');產生的圖與字符串float1winter關聯起來。如果你想指定地塊與圖例項去,你需要將地塊的對象句柄傳遞給legend,這是最簡單的通過返回的句柄完成後你plot最初:

h(1) = plot(float1winter.T,float1winter.P,'b');  
hold on; 
h(2) = plot(float1summer.T,float1summer.P,'r'); 
h(3) = plot(tempAdiff,float1summer.P,'--k'); 
legend(h,{'11-29-2013','07-01-2013','Temperature Difference'}); 

旁註:你只需要每個軸調用一次hold on - 因此每調用一次subplot但不是每調用一次plot

或者,你根本不能給柄; legend將分配文本情節的順序,他們畫在:

legend({'11-29-2013','07-01-2013','Temperature Difference'}) 

瞭解圖形處理可以讓你在地塊更大的控制權,特別是如果你可能要進行小的調整給他們。例如,如果我決定,我想第一個情節是綠色的,而不是藍色的,那麼我可以這樣做:

set(h(1),'Color','g'); 

這將改變繪製顏色傳說的變化,自動將匹配。要查看對象的所有屬性的列表,請僅使用手柄使用get。您可以一次多個房產set。例如:

get(h(1)) 
set(h(1),'DisplayName','Winter','LineWidth',3,'Marker','x')