how to xlimit for 3 different plot in 1 figure?

I need to set xlimit for 3 different plot that there are in 1 figure....i set xlimit but it will set for all

 Accepted Answer

Star Strider
Star Strider on 31 Jan 2024
Edited: Star Strider on 31 Jan 2024
The plots seem not to have been posted, so I am not certain what you want to do.
You would need to set xlim for each one individually, assuming they are each set in separate axes.
Otherwise, you would need to plot each y-vector with a separate x-vector of the same size, although with different start and end values.

8 Comments

Yep not posted...my bad...No they are in 1 axes and i want set for example 0 to 9 for first plot and 2 to 10 for next and...
And the problem is i have figure file not the data...
You would then need to extract the data from the figure first. That is not trivial, however it is not difficult.
Try this —
x = linspace(1,10,20);
y1 = sin(2*pi*x/7.5);
y2 = cos(2*pi*x/20);
figure
plot(x, y1)
hold on
plot(x, y2)
hold off
grid
title('Original')
savefig('Test.fig') % Save Original
F = openfig('Test.fig'); % Open Original
Lines = F.Children.Children;
for k = 1:numel(Lines) % Recover Line Data
xr{k} = Lines(k).XData;
XL(k,:) = [min(xr{k}) max(xr{k}) numel(xr{k})];
yr{k} = Lines(k).YData;
end
xlims = [0 9; 2 10]; % Set 'x' Limits
for k = 1:numel(yr)
x1{k} = linspace(xlims(k,1), xlims(k,2), XL(k,3)); % Create New 'x' Vectors
end
figure
hold on
for k = 1:numel(yr)
plot(x1{k}, yr{k}) % Plot New 'x' Vectors & Recovered 'y' Vectors
end
hold off
grid
title('Shifted Lines')
That approach should work.
.
i wanna add 'a' ito that figure and 'It' should e 28 to 328 and 'a' should be 0 to 601
Here you go — ‘It’ is shifted appropriately, and ‘a’ is added to the right y-axis —
LD = load('matlab.mat');
a = LD.a;
F = openfig('untitled.fig'); % Open Original
Kids = (F.Children);
Lgnd = F.Children(1);
Axs = F.Children(2);
% get(Axs)
Lines = Axs.Children
Lines =
3×1 Line array: Line (ir) Line (ic) Line (It)
% Lines(3)
AllAx = findobj(F, 'Type','Axes');
% get(AllAx)
% AllAx.XAxis
% YLeft = AllAx.YAxis(1)
% YRight = AllAx.YAxis(2)
% get(YRight)
for k = 1:numel(Lines) % Recover Line Data
xr{k,:} = Lines(k).XData;
XL(k,:) = [min(xr{k}) max(xr{k}) numel(xr{k})];
yr{k,:} = Lines(k).YData;
end
xlims = [XL(1:2,1:2); 28 328]; % Set 'x' Limits
for k = 1:numel(yr)
x1{k} = linspace(xlims(k,1), xlims(k,2), XL(k,3)); % Create New 'x' Vectors
end
Lines(3).XData = x1{3}; % 'Lines(3)' Is 'It'
hold on
yyaxis right
plot((0:600), a, '--m', 'DisplayName','a')
hold off
Change the colour, linestyle, and 'DisplayName' for ‘a’ to get the result you want.
You will need to expand the legend to accommodate ‘a’.
This was definitely not as straightforward as I thought it would be.
EDIT — Corrected typographical errors.
.
What i see in pic is not correct idk maybe i described wrong what i needed...in the end 'It' and 'a' should start and end in same spot see a is 3 cycle and It is 6 cycle...thats why i wanna cut the half of It...they should both match
You originally requested:
i wanna add 'a' ito that figure and 'It' should e 28 to 328 and 'a' should be 0 to 601
That does not match what you just wrote:
in the end 'It' and 'a' should start and end in same spot see a is 3 cycle and It is 6 cycle
As I posted in my plot, ‘a’ (the dashed magenta line) has 3 cycles and spans the entire range. The ‘It’ vector has nearly 6 complete cycles and goes, as you requested, from 28 to 328.
The XData and YData properties for ‘It’ would have to be changed to:
Lines(3).XData = linspace(0, 600, 301);
Lines(3).YData = Lines(3).YData(28:328);
After that, ‘a’ and ‘It’ are similar although do not exactly correspoind to each other.
Changing it slightly to accommodate that —
LD = load('matlab.mat');
a = LD.a;
F = openfig('untitled.fig'); % Open Original
Kids = (F.Children);
Lgnd = F.Children(1);
Axs = F.Children(2);
% get(Axs)
Lines = Axs.Children
Lines =
3×1 Line array: Line (ir) Line (ic) Line (It)
% Lines(3)
AllAx = findobj(F, 'Type','Axes');
Lines(3).XData = linspace(0, 600, 301); % 'Lines(3)' Is 'It'
Lines(3).YData = Lines(3).YData(28:328);
hold on
yyaxis right
plot((0:600), a, '--m', 'DisplayName','a')
hold off
This does what you requested, using the specifications (specifically ‘28 to 328’ for ‘It’) that you stated.
.
Sorry for mis understanding and million Thx to u sir
As always, my pleasure!
No worries!

Sign in to comment.

More Answers (2)

In your comment you said "I have figure file not the data."
So, starting with a FIG-file, you first need to find the axes within the FIG file:
f = openfig(filename); % Where filename is the path to the FIG file.
ax = findobj(f,'Type','Axes');
Once you've found the axes, if you need to set them all to use the same limits, you can do that in one command:
xlim(ax, [0 9]) % Where [0 9] are the new limits.
If you need to set the limits on each axes different, it is a bit more complicated, because you need to identify which axes is which. There are several ways to do that. If they have titles, you are in luck, because you can just display the axes handles at the command line to see the titles for each axes. If you just run "ax" (the name of the variable) on the command line, you will get a list of all the axes with their titles.
ax
Once you've done that, you can pick which one you want to have each limits and set them.
xlim(ax(1), [0 9]);
xlim(ax(2), [2 10]);
% etc.
If you don't have titles on the axes, you need to determine which axes handle corresponds to which axes. There are a few options, but they all boil down to looking for (or adding) some distinguishing characteristic.
  1. Set the Color of each axes, temporarily, to some distinguishing color, so you can see which one you have a handle to, then set it back to the original color (defaults to white).
  2. Check the existing XLim or YLim (if they are different on different axes).
  3. Add titles to each axes, then use the above technique.
  4. Set the Visible property on one axes to 'off', see which one disappears, then set Visible back to 'on'.
  5. Or just pick one, set the XLim, if you picked the wrong one, you will see which one changed, and can set it to the correct value.

1 Comment

i wanna add 'a' ito that figure and 'It' should e 28 to 328 and 'a' should be 0 to 601....the prolem is first one has 300 value and secod one has 601...is that even possible?can i do that?

Sign in to comment.

% Example plot
subplot(3,1,1)
plot(rand(10,10))
subplot(3,1,2)
plot(rand(10,10))
subplot(3,1,3)
plot(rand(10,10))
% Set the xlimits to be 0 to 9 for each plot
ax = findobj(gcf,'Type','Axes');
for i = 1:length(ax)
ax(i).XLim = [0,9];
end

1 Comment

You don't need the for loop:
ax = findobj(gcf,'Type','Axes');
xlim(ax, [0 9]);

Sign in to comment.

Products

Release

R2016b

Tags

Community Treasure Hunt

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

Start Hunting!