How to specify same positive and negative axes-limits without knowing the limit values beforehand?

조회 수: 8 (최근 30일)
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.

답변 (2개)

David Goodmanson
David Goodmanson 2018년 2월 20일
편집: David Goodmanson 2018년 2월 20일
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])

Chris Wilkening
Chris Wilkening 2024년 5월 9일
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))])

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by