How do I set a limit for a line on a plot?

51 views (last 30 days)
Salar
Salar on 10 Mar 2016
Commented: Ced on 11 Mar 2016
Hello,
I need to set a limit for a line on my plot. I have couple of different lines on the same plot and I need only one of them to be displayed only between a certain Xmin and Xmax. I'm not looking for axis limi because that effects my other lines as well. Thank you for your help!
limits=[-7 7 -5 5];
axis(limits)
hold on
plot(228.63.*y,-660.*z,'k')
plot(y, .34630912.*y-2.63399957,'g')
plot(2.85788.*y,-1.65.*z,'r')
for example here the second plot command. I need to set a limit for that one.

Answers (1)

Ced
Ced on 10 Mar 2016
Edited: Ced on 10 Mar 2016
Matlab is going to plot whatever you pass it. So e.g. if you want your plot only to exist between -2 and 2, then you should pass the data accordingly.
Mini example:
x = -1:0.1:1; % -1 < x < 1
y_long = sin(x);
x_short = x(x>=0); % we only want x >= 0
y_short = sin(x_short);
figure()
hold on
plot(x,y); % here, x goes from -1 to 1
plot(x_short,y_short); % here, x goes only from 0 to 1
PS: I doubt that you need this, but for completeness:
If you already have a plot, you could extract the data through get(plot_handle,'XData') etc., adjust it as desired and then set it again through set(plot_handle,'XData') etc. Don't forget to run drawnow() afterwards.
  3 Comments
Salar
Salar on 11 Mar 2016
I have tried this, but this doesn't bound the line on both sides for some reason.
y_short = y(y>=0 & y<=2.85788)
Ced
Ced on 11 Mar 2016
But that's exactly how to do it. However, if you want to bound x, you should do this with x, not y. Or was it just a typo?
Adjusting the example above, this gives you 0 <= x <= 0.5
x = -1:0.1:1; % -1 < x < 1
y_long = sin(x);
x_short = x(0.5 >= x & x>=0); % we only want 0.5 >= x >= 0
y_short = sin(x_short);
figure()
hold on
plot(x,y_long); % here, x goes from -1 to 1
plot(x_short,y_short); % here, x goes only from 0 to 1

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!