set same xlim for all subplots

392 次查看(过去 30 天)
Hi all,
i need to specify one "xlim" for my all multiple subplots. I read already answers here but i got some problems about my labels. It disappear. I think; because of "set(gca)" on my codes. I couldn't figure out, how to solve this. If you any idea i really appreciated that. Here my codes of a subplot. Thank you so much!
  5 个评论
Olga Zinovieva
Olga Zinovieva 2019-6-14
编辑:Olga Zinovieva 2019-6-14
I've tried to implement the approach proposed by dpb and got an error (R2015a)
Error using xlim. Wrong number of arguments.
xlim line was basically the same
xlim(hAx,[0 150])
dpb
dpb 2019-6-14
编辑:dpb 2019-6-14
Hmmm...I would have thought xlim was vectorized earlier than that but apparently not--R2014b is latest I presently have installed prior to R2017b which works and it also fails (not unexpectedly given R2015a did).
Have to use the set alternative then...
set(hAx,'xlim',[0 150])

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2018-7-29
You may want to use linkaxes on your subplot axes.
  6 个评论
Steven Lord
Steven Lord 2018-7-30
As Adam said, the axes you link don't have to have the same figure as their Parent.
% Make the first axes in its figure
f = figure;
ax = axes('Parent', f);
% Plot some data into the first axes
x = 0:0.1:2*pi;
plot(ax, x, sin(x));
% Make a second axes in a second figure
f2 = figure;
ax2 = axes('Parent', f2);
% Plot some data into the second axes
plot(ax2, x, cos(x));
% Link the x axis of the two axes together
linkaxes([ax, ax2], 'x')
Now move the figures so you can see both of the axes, turn panning on in one of the figures, and pan one of the axes. The other axes will pan so its x axis matches the one in which you're manually panning. Since I only linked the x axis of the two axes, the y axis of the two axes might start to differ both because you're panning manually and probably not panning only horizontally and because the limits of the other axes will default to automatically spanning the data visible in that axes.
Ase U
Ase U 2018-7-31
thank you so much, it worked perfect!

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2018-7-29
Just get the min and max of all x limits. See this demo:
subplot(2,2,1);
% Plot something...
plot(rand() * 1000 * rand(10,1), rand(10,1));
% Find xLimits for this graph.
xl1 = xlim
subplot(2,2,2);
% Plot something...
plot(rand() * 1000 * rand(10,1), rand(10,1));
% Find xLimits for this graph.
xl2 = xlim
subplot(2,2,3);
% Plot something...
plot(rand() * 1000 * rand(10,1), rand(10,1));
% Find xLimits for this graph.
xl3 = xlim
subplot(2,2,4);
% Plot something...
plot(rand() * 1000 * rand(10,1), rand(10,1));
% Find xLimits for this graph.
xl4 = xlim
% Find leftmost xLeft
xLeft = min([xl1(1), xl2(1), xl3(1), xl4(1)])
% Find rightmost xRight
xRight = max([xl1(2), xl2(2), xl3(2), xl4(2)])
uiwait(msgbox('See it now'));
% Set all to be the same
subplot(2,2,1);
xlim([xLeft, xRight]);
subplot(2,2,2);
xlim([xLeft, xRight]);
subplot(2,2,3);
xlim([xLeft, xRight]);
subplot(2,2,4);
xlim([xLeft, xRight]);
  2 个评论
Ase U
Ase U 2018-7-29
thanks for your nice answer. But my signal like in between -200 and 200 seconds and i just need to see from 0 to 50 seconds. Max and min values are large. Do you have any another advice?
Image Analyst
Image Analyst 2018-7-29
Did you see how I used the xlim() function? There is help documentation on it if you need more examples. For you, you can do
xlim([0, 50]);
Similarly if you want to set the limits on the range in the y direction.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by