2012-01-24 27 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