set same xlim for all subplots

515 visualizaciones (últimos 30 días)
Ase U
Ase U el 29 de Jul. de 2018
Editada: Matthew el 6 de Oct. de 2024
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 comentarios
dpb
dpb el 14 de Jun. de 2019
Editada: dpb el 14 de Jun. de 2019
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 el 6 de Oct. de 2024
Editada: Matthew el 6 de Oct. de 2024
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])

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 29 de Jul. de 2018
You may want to use linkaxes on your subplot axes.
  6 comentarios
Steven Lord
Steven Lord el 30 de Jul. de 2018
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 el 31 de Jul. de 2018
thank you so much, it worked perfect!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 29 de Jul. de 2018
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 comentarios
Ase U
Ase U el 29 de Jul. de 2018
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 el 29 de Jul. de 2018
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.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by