2012-01-24 28 views
0

我必須繪製一些數據,我需要兩個x和y軸。 主要的x和y給了我位移信息,次要的(x在上面和y在右邊)給了我關於能量的信息。 我遇到的問題是,如果我使繪圖窗口更大,次軸不能正確調整大小,但它非常小,繪圖標題寫在工具欄下方,我只能看到字母的下半部分。 有人知道如何解決第二軸上的主要問題?多軸圖

我用於輔助軸的代碼是:

figure(1) 

%%%%voglio fare un plot tenendo fisse le dimensioni delle icone nella legenda 
hplot = plot(yH(1,:),xH(1,:),'^', yC(:,1),xC(:,1),'*',yC(:,2),xC(:,2),'*',... 
    yC(:,3),xC(:,3),'*',yC(:,4),xC(:,4),'*',yC(:,5),xC(:,5),'*',... 
    yC(:,6),xC(:,6),'*','MarkerSize',s); % Markersize: specifys the size of the marker in points (s in questo caso) 
hold on 
plot(Ymcporigine,Xmcporigine,'k-','MarkerEdgeColor','k','MarkerSize',1); %Plot contorno MCP 
hold on 
plot(Yh, Xh, 'b-', 'MarkerSize', s); %Plot alone circolare 
hold off 

%Labe assi principali - It is necessary to give the label instructions after plot in order to avoid overlap 
xlabel(gca, 'Deflessione magnetica [m]'); % label lower x axis 
ylabel(gca,'Deflessione elettrica [m]'); %label left y axis 

%particles outside MCP radius won't be appear in figure 
xlim([0, Rmcp]) 
ylim([0, Rmcp]) 

%%%% legenda assi principali 
l=legend(hplot, 'H^+','C^+','C^{+2}','C^{+3}','C^{+4}','C^{+5}','C^{+6}', 'Location','BestOutside'); 
a=get(l,'children'); 
set(a(1:3:end),'MarkerSize',10); 


%%%% doppio Asse x 
%xlabel(gca, 'Deflessione magnetica [m]'); % label asse x principale 

%set secondary x limit as the momentum of a H+ at distance equal to the MCP radius 
% Secondo Harres y=(q*B*LB*L)/sqrt(2mEkin) ==> mv=q*B*LB*L/y 
mv_max = (q*B*LB*L)/Rmcp; 
%mv_max = 1; 

%Layout instruction 
set(gca,'Box','off'); % Turn off the box surrounding the whole axes 
axesUnits=get(gca,'Units'); 
axesPosition = get(gca,'Position');   %# Get the current axes position 
hNewAxes = axes('Position',axesPosition,... %# Place a new axes on top... 
       'Units', axesUnits,... 
       'ActivePositionProperty', 'OuterPosition',... 
       'Color','none',...   %# ... with no background color 
       'XAxisLocation','top',... %# ... located on the top 
       'Ytick', [],...    %# ... with no y tick marks 
       'Xlim', [0, mv_max],...  %# ... should define x axis scale (need to set xmax = mv_max) 
       'Box','off');    %# ... and no surrounding box 

      xlabel(hNewAxes,'Momentum (H^+)'); %# Add a label to the top axis 
set(gca, 'XTickLabel', num2str(get(gca,'XTick')','%g')) 

%%%%%Plot title - It is necessary to give the title instruction after secondary x axis in order to avoid overlap   
title(['Calcolo approssimato interazione ioni campo magnetico B=', num2str(B), 'Tesla']); 

%%%% doppio Asse y    
%ylabel(gca,'Deflessione elettrica [m]'); %label asse y principale 

%set secondary y limit as the energy of a H+ at distance equal to the MCP radius 
% Secondo Harres x=(q*E*Le*L)/(2mEkin) ==> Ekin=q*E*Le*L/2mx 
Le = 0.07; %Estensione del C.E. : 70 mm 
E = 100000; %campo TP.m 
Ekin_max = (q*E*Le*L)/(2*m_H*Rmcp); 
%mv_max = 1; 

set(gca,'Box','off'); % Turn off the box surrounding the whole axes 
axesUnits = get(gca,'Units'); 
axesPosition = get(gca,'Position');   %# Get the current axes position 
hNewAxes = axes('Position',axesPosition,... %# Place a new axes on top... 
       'Units', 'normalized',... 
       'ActivePositionProperty', 'OuterPosition',... 
       'Color','none',...   %# ... with no background color 
       'YAxisLocation','right',... %# ... located on the right 
       'Ylim', [0, Ekin_max],... %# ... should define y axis scale (need to set ymax=Ekin_max) 
       'Xtick', [],...    %# ... with no y tick marks 
       'Box','off');    %# ... and no surrounding box 

      ylabel(hNewAxes,'Energy (H^+)'); %# Add a label to the top axis 
set(gca, 'YTickLabel', num2str(get(gca,'YTick')','%g')) 
+0

有沒有可能獲得完整的代碼來重現問題? –

+0

好吧,我已經發布了我用於整個劇情的代碼。如果你需要它可以爲變量提供微積分的部分,我也可以提供。 – user1167516

回答

0

從MATLAB文檔看起來你應該設置ActivePositionPropertyOuterPosition,而不是Position。我無法重現縮放問題,但設置似乎確實調整了標題位置。

+0

我已經使用了「OuterPosition」,當我把窗口放大時,標題會下來,但是我必須組織輔助軸,因爲它們是亂七八糟的。 – user1167516

1

我記得在Matlab文檔中看到這個walkthrough on how to set up multiple axes。我嘗試了他們的示例代碼,並且一切正常。

如果您的代碼與Matlab文檔不同,您需要同時定義x軸和y軸,而不是兩個不同的新軸語句。所以,拿出你的兩個hNewAxes語句和一個替換,包括所有的屬性:

hNewAxes = axes('Position',axesPosition,... %# Place a new axes on top... 
      'Units', 'normalized',... 
      'ActivePositionProperty', 'OuterPosition',... 
      'Color','none',...   %# ... with no background color 
      'YAxisLocation','right',... %# ... located on the right 
      'XAxisLocation','top',... 
      'Xlim', [0, mv_max],... 
      'Ylim', [0, Ekin_max],... %# ... should define y axis scale (need to set ymax=Ekin_max) 
      'Box','off');    %# ... and no surrounding box 

如果你想繪製一些關於你的數據線的一組軸,以及一些關於第二,那麼對於第二組你必須繪製他們在演練結束奠定了風格:

hl2 = line(x2,y2,'Color','k','Parent',ax2); 

在「父」屬性告訴Matlab的哪些軸使用。

+0

我不能區分這個示例和我的代碼,除了我有更多的指令...抱歉,但我是一個編程新手。 – user1167516

+0

差不多完成了。我仍然有調整大小的問題,但在列表中更容易手動調整它們 – user1167516