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

9 ビュー (過去 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 Center および File ExchangeSubplots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by