필터 지우기
필터 지우기

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

조회 수: 45 (최근 30일)
Salar
Salar 2016년 3월 10일
댓글: Ced 2016년 3월 11일
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에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by