setting the axis min and max values to stick
axes, matlab, subplot
Solution
To programmatically set axis boundaries there are a few useful commands:
axis([0 30 0 8]); %Sets all four axis bounds
or
xlim([0 30]); %Sets x axis limits
ylim([0 8]); %Sets y axis limits
To only set one of the two x limits I usually use code like this:
xlim([0 max(xlim)]); %Leaves upper x limit unchanged, sets lower x limit to 0
This takes advantage of `xlim`s zero input argument calling convention, which returns an array of the current x limits. The same works with `ylim`.
Note that all of these commands apply to the current axis, so if you are creating subplots you will need to perform the scaling call once per axis as you build up your figure.
Another useful fatures is the `linkaxes` command. This dynamically links the axis limits of two plots, including for programmatic resize commands like `xlim` and UI operations like pan and zoom. For example:
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.
Looking at your code, post edit, your use of the `plotmatrix` function is complicating things. `plotmatrix` appears to create its own axes to work in, so you need to capture those handles and adjust them. (Also, in the future take `h = zeros(..)` out of the loop).
To get the handles to the `plotmatrix` created axes, use the second return argument, like this: `[~, hAxes]=plotmatrix(current_rpm,current_torque);`. Then collect those for future use.
Finally, the `axis`, `xlim`, `ylim` commands all act on the current axis, (see `gca`). However the `plotmatrix` axes are never current, so the `axis` command has not been affecting them. You can specify the axis to act on, like this: `axis(hAxis, [0 30 0 8]);`.
Putting this all together (an adding some variable definitions to get your code to execute), and this is what it looks like:
%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]);
Problem
I have a (3,4) subplot each showing scatterplots. The ranges of the scatterplots vary so some of my plots have axes x(0-30) and y(0-8), but some have x(18-22) and y(4-7). I have set my xlim to [0 30], and ylim to [0 8] but that sets my axes to never go lower than 0, higher than 30 etc. How do I set my axis to "stick" at (0,0) for the origin of each plot, and "stick" at 8 for Y and 30 for X. TIA for any help update per comment on answer: Still having the same issue with below code ``` %% plot for i = 1:num_bins; h = zeros(ceil(num_bins),1); h(i)=subplot(4,3,i); plotmatrix(current_rpm,current_torque) end linkaxes(h,'xy'); axis([0 30 0 8]); ```