2016-10-25 16 views
0

ベースコードhereから、Figureをプロットし、Figureの上と下の両方にx軸のラベルを付ける関数を作成しました。左右のy軸ラベル。MATLAB:プロットの各辺にxとyのラベルを付ける問題

enter image description here

:私の問題は、私はラベルが上書きされ、何らかの理由で、y軸のラベルを示し、 最初の実行などの奇妙な方法で上書きゲットするコードを複数回実行する必要があり、その都度ということです

セカンドラン:

enter image description here

次はMWEです:

% sample data, plot 
x=[1:168]; 
y=x; 
plot(x, y, 'r', 'LineWidth', 1); 


set(gca, 'XTick', [], 'YTick', []); 

% set left yaxis label 
ylabel(directions{1,1},'Rotation',-360); 

% Adjust position - this seems to be what's causing the issue! 
ylabelh = get(gca,'YLabel'); 
rpos = get(ylabelh,'Position'); 
set(ylabelh,'Position',rpos + [1.5*rpos(1) 0 0]) 

% do stuff... as per link above 
axesPosition = get(gca,'Position'); 
hNewAxes = axes('Position',axesPosition,... %# Place a new axes on top... 
      'Color','none',...   %# ... with no background color 
      'YAxisLocation','right',... %# ... located on the right 
      'XTick',[],'YTick',[],...   %# ... with no x tick marks 
      'Box','off'); 

ylabel(hNewAxes,directions{2,1},'Rotation',-360); % yaxis label right 

% Adjust position 
ylabelh = get(gca,'YLabel'); 
Lpos = get(ylabelh,'Position'); 
set(gca,'YTick',[]); 
set(ylabelh,'Position',Lpos+ [+Lpos(1)*0.02 0.05 0]) 

% -- And repeat for x axis -- % 

% x axis labels 
xlabel(directions{3,1},'Rotation',-360); % xaxis label bottom 
% Adjust position 
xlabelh = get(gca,'XLabel'); 
xlabpos = get(xlabelh,'Position'); 
set(gca,'XTick',[]); 
rpos = get(xlabelh,'Position'); 
% do stuff ... as above 
axesPosition = get(gca,'Position'); 
hNewAxes = axes('Position',axesPosition,... %# Place a new axes on top... 
      'Color','none',...   %# ... with no background color 
      'XAxisLocation','top',... %# ... located on the right 
      'XTick',[],'YTick',[], ...   %# ... with no x tick marks 
      'Box','off'); 

% xaxis label top 
xlabel(hNewAxes,directions{4,1},'Rotation',-360); 

奇妙なことに、スクリプトとしてコードを実行すると(ラベルは上書きされますが、y軸のラベルに問題はありません)、デバッグモードで複数回実行すると私がそれを使用している現在の主な方法)、私は上記の人工物を見る。

答えて

0

コードを実行すると、最後にスクリプトに3つの軸が作成されていることがわかります。一度実行すると、get(gcf,'Children')と入力します。もう一度実行すると別の2軸が追加されますが、これは毎回実行します。

どうしてですか?

あなたのコードでは、2つの新しい軸ハンドル(同じ名前を - これはあなたがしてはならない - >これは呼び出さないでください)を作成しました - ただし、すでに使用しているのはgca-> )。

コードを完全に再構成することをお勧めします。プロット/ラベリングなどを行う前に、2軸を作成することから始めます。2軸の変数を独立して保存します。必要に応じて参照することができます。

gcaまたはgcfを現在の軸または図を取得するために決して頼りないようにするのもよい方法です。軸/図形ハンドルを格納し、それらを明示的に参照します。

関連する問題