MATLAB Answers

Ano
0

plotting issue along the x-axis !

Asked by Ano
on 9 Dec 2017
Latest activity Commented on by Star Strider
on 11 Dec 2017
Hello ! I would like to plot two curves in the same figure, to make my issue more clear let consider the following example:
x = linspace(0,10,20);
y = x ;
y1 = x;
plot(x,y);hold on
plot(-x,y1) ;
I would like to get the same general aspect of the figure but with the second curve plotted along [10 0], any suggestions how can I get it done ?! Thank you!

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Star Strider
on 9 Dec 2017
 Accepted Answer

Use fliplr (or flip) on the x argument in the second plot call:
x = linspace(0,10,20);
y = x ;
y1 = x;
plot(x,y)
hold on
plot(fliplr(x),y1)
hold off

  8 Comments

In the code in my previous Comment, I used the same function plotted over the negative and positive axes, and then took the absolute value of it and re-labeled the x-axis.
If you want to plot two different functions on the same axes, you can do what I did here (plot the absolute value of the second function over the negative axis), then re-label the axes.
Plotting the absolute value of the second function and then re-labeling the axes appears to be the result you want.
Example
x = linspace(-2.1, 10, 51);
y1 = x;
y2 = abs(x).^3;
figure(1)
plot(x(x>=0), y1(x>=0))
hold on
plot(x(x<=0), y2(x<=0))
hold off
xt = get(gca, 'XTick');
lxt = length(xt);
set(gca, 'XTickLabel', [-(xt(1:fix(lxt/2))) xt(fix(lxt/2)+1:end)])
This appears to be reasonably robust.
thank you very much !
As always, my pleasure!

Sign in to comment.