set same xlim for all subplots

조회 수: 436 (최근 30일)
Ase U
Ase U 2018년 7월 29일
편집: Matthew 2024년 10월 6일
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!
  댓글 수: 6
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])
Matthew
Matthew 2024년 10월 6일
편집: Matthew 2024년 10월 6일
dbp, a scenario where this arises is if I want to create two figures each containing subplots where the only difference between the two figures is their different scales. I used
f=copyobj(gcf,0)
to duplicate the figure, but I didn't have the axis handles of the new figure to adjust the xlim of all axes in the new figure.
To obtain the axis handles in order to do this... if "f" is the figure handle that contains all the subplots you want to adjust, you can get all the axes using findall, and then pass that to xlim when setting. Hope this helps the next user
ax = findall(f,'type','axes');
xlim(ax, [0 300])

댓글을 달려면 로그인하십시오.

채택된 답변

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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by