軸の境界をプログラムで設定するには、いくつかの便利なコマンドがあります。
axis([0 30 0 8]); %Sets all four axis bounds
また
xlim([0 30]); %Sets x axis limits
ylim([0 8]); %Sets y axis limits
2 つの x 制限の 1 つだけを設定するには、通常、次のようなコードを使用します。
xlim([0 max(xlim)]); %Leaves upper x limit unchanged, sets lower x limit to 0
これはxlim
、現在の x 制限の配列を返すゼロ入力引数呼び出し規則を利用します。同じことが動作しylim
ます。
これらのコマンドはすべて現在の軸に適用されることに注意してください。したがって、サブプロットを作成している場合は、Figure を構築するときに軸ごとにスケーリング呼び出しを 1 回実行する必要があります。
もう 1 つの便利な機能はlinkaxes
コマンドです。これにより、2 つのプロットの軸の制限が動的にリンクされます。これには、プログラムによるサイズ変更コマンドxlim
や、パンやズームなどの UI 操作が含まれます。例えば:
a(1) = subplot(211),plot(rand(10,1), rand(10,1)); %Store axis handles in "a" vector
a(2) = subplot(212),plot(rand(10,1), rand(10,1)): %
linkaxes(a, 'xy');
axis([0 30 0 8]); %Note that all axes are now adjusted together
%Also try some manual zoom, pan operations using the UI buttons.
コードを見ると、編集後、plotmatrix
関数の使用が複雑になっています。 plotmatrix
作業する独自の軸を作成しているように見えるため、これらのハンドルをキャプチャして調整する必要があります。(また、将来的h = zeros(..)
にはループから抜け出します)。
作成された軸へのハンドルを取得するにはplotmatrix
、次のように 2 番目の戻り引数を使用します[~, hAxes]=plotmatrix(current_rpm,current_torque);
。次に、将来の使用のためにそれらを収集します。
最後に、axis
、xlim
、ylim
コマンドはすべて現在の軸に作用します ( を参照gca
)。ただし、plotmatrix
軸は決して最新ではないため、axis
コマンドはそれらに影響を与えていません。次のように、作用する軸を指定できます axis(hAxis, [0 30 0 8]);
。
これをすべてまとめると (コードを実行するためにいくつかの変数定義を追加します)、これは次のようになります。
%Define some dummy variables
current_rpm = rand(20,1)*30;
current_torque = rand(20,1)*8;
num_bins = 12;
%Loop to plot, collecting generated axis handles into "hAllAxes"
hAllAxes = [];
for i = 1:num_bins;
subplot(4,3,i);
[~, hCurrentAxes]=plotmatrix(current_rpm,current_torque);
hAllAxes = [hAllAxes hCurrentAxes]; %#ok
end
linkaxes(hAllAxes,'xy');
axis(hAllAxes,[0 30 0 8]);