How to specify same positive and negative axes-limits without knowing the limit values beforehand?
9 views (last 30 days)
Show older comments
Is there some way we can specify the axes to have uniform limits on positive and negative side without knowledge of the limits themselves. I am generating several figures at once with multiple subplots in each figure, and my x-axis limits range from [-4 3] for one subplot to [-25 20] for another subplot. I want each subplot to have same limits on both sides of zero, i.e. they should adjust to [-4 4] and [-25 25] on their own, especially since I don't know the actual values until the graphs are plotted. It is impractical for me to do this for each figure and subplot with the number I generate. Keep in mind that I do not want all my subplots to have the same limits as each other, just the same positive and negative limits. This is urgent and any help is much appreciated.
0 Comments
Answers (2)
David Goodmanson
on 20 Feb 2018
Edited: David Goodmanson
on 20 Feb 2018
Hi Siddharth,
Here is the simplest way, which assumes that the elements in the x array are all increasing or all decreasing, as usual. You can experiment around with the factor of 1.1; a factor of 1 works all right too.
There is a more complicated way that finds the limits set by plot and adjusts those. You get slightly better looking graph limits sometimes, but I don't know that the added complication is worth it.
x = -4:.01:14.1;
y = cos(x);
figure(1)
plot(x,y)
maxlim = max(abs([x(1) x(end)]))
xlim(1.1*[-maxlim maxlim])
0 Comments
Chris Wilkening
on 9 May 2024
Seems like this should be an option but below works
x = -4:.01:14.1;
y = cos(x);
figure(1)
plot(x,y)
xlim([-max(abs(xlim)), max(abs(xlim))])
ylim([-max(abs(ylim)), max(abs(ylim))])
0 Comments
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!