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

49 次查看(过去 30 天)
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.

回答(1 个)

Ced
Ced 2016-3-10
编辑:Ced 2016-3-10
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 个评论
Salar
Salar 2016-3-11
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 2016-3-11
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by