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 Kommentare

dpb
dpb am 29 Jul. 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...
Ase U
Ase U am 29 Jul. 2018
Bearbeitet: Ase U am 29 Jul. 2018
Hi! Yes, i have multiple plots but i wanted to share just one example, because; the others subplots have the same code, datas are different and so i did just copy paste. For each subplot i have xlim, but i want to make that with one command.
And i find here this solution -->
axH = findall(gcf,'type','axes');
%# set the x-limits of all axes (see axes properties for %# more customization possibilities)
set(axH,'xlim',[0 50])
But, if i use that i cant see anymore my labels name. I hope i could explain well. Thank you for your answer.
dpb
dpb am 29 Jul. 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
Olga Zinovieva am 14 Jun. 2019
Bearbeitet: Olga Zinovieva am 14 Jun. 2019
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 am 14 Jun. 2019
Bearbeitet: dpb am 14 Jun. 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 am 6 Okt. 2024
Bearbeitet: Matthew am 6 Okt. 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])

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Steven Lord
Steven Lord am 29 Jul. 2018

3 Stimmen

You may want to use linkaxes on your subplot axes.

6 Kommentare

dpb
dpb am 29 Jul. 2018
That, too, ... :)
Ase U
Ase U am 30 Jul. 2018
Hi Steven, thank you for your help, i used this method. And do you know how can i use then "linkaxes" not only for subplots but also for figures too?
Adam
Adam am 30 Jul. 2018
linkaxes works on axes. The concept of linking figures doesn't make sense. The axes handles you link do not have to be on a single figure though, they can be across multiple figures.
Ase U
Ase U am 30 Jul. 2018
i dont unterstand. I have 4 figures and each figure has 4 subplots. I want to see same x limit on every plot.
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 am 31 Jul. 2018
thank you so much, it worked perfect!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 29 Jul. 2018

2 Stimmen

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 Kommentare

Ase U
Ase U am 29 Jul. 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?
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.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 29 Jul. 2018

Bearbeitet:

am 6 Okt. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by