set same xlim for all subplots
Mostrar comentarios más antiguos
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
el 29 de Jul. de 2018
You only show one subplot() but speak of "multiple"
There's no reason not to simply place
xlim([0 50])
in each subsection generating each subplot you wish with that limit.
In general, you would be wise to save the axes handle for each subplot when you create it so that you can refer to it programmatically when needed and avoid gca when there are multiple axes so you're sure you're talking to the expected one.
There's insufficient information from which to draw any conclusion as to why anything would "disappear", we would need to see enough code to generate an example of what you see in order to do that. The most common problem in such cases is that not all the data are on the same scale so changing the xlim property simply moves the axes to some position for which there is no data in that range. But, w/o two or more and no specific data we can't know...
dpb
el 29 de Jul. de 2018
Well we can't debug what we can't see, which is what might have caused the symptom. Labels don't have anything to do with axis limits, though, so you may well be barking up the wrong tree regarding the symptoms (did I mention we can't see those?)
There's really very little to be gained by the one call to xlim over the call within each section but again as noted, keep the axes handles when you create them; then you can use the array of handles...
hAx=gobjects(2*2,1); % preallocate for the axes handles
for i=1:4
hAx(i)=subplot(2,2,i);
...
% plot into i-th subplot here
end
xlim(hAx,[0 50])
will set all axes to the desired limits.
Olga Zinovieva
el 14 de Jun. de 2019
Editada: Olga Zinovieva
el 14 de Jun. de 2019
Error using xlim. Wrong number of arguments.
xlim line was basically the same
xlim(hAx,[0 150])
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])
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])
Respuesta aceptada
Más respuestas (1)
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
el 29 de Jul. de 2018
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.
Categorías
Más información sobre Axis Labels en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!