Filter löschen
Filter löschen

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

45 Ansichten (letzte 30 Tage)
Salar
Salar am 10 Mär. 2016
Kommentiert: Ced am 11 Mär. 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.

Antworten (1)

Ced
Ced am 10 Mär. 2016
Bearbeitet: Ced am 10 Mär. 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 Kommentare
Salar
Salar am 11 Mär. 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 am 11 Mär. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by