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!

 Akzeptierte Antwort

Star Strider
Star Strider am 9 Dez. 2017

0 Stimmen

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 Kommentare

Ano
Ano am 9 Dez. 2017
thank you for your answer but it didn't work , actually I would like to have an x-axis along [0 10] but with two directions as showing in the figure attached
Star Strider
Star Strider am 9 Dez. 2017
Bearbeitet: Star Strider am 11 Dez. 2017
The easiest way is to simply re-label the x-axis:
x = linspace(0,10,20);
y = x ;
y1 = x;
plot(x,y)
hold on
plot(-x,y1)
hold off
set(gca, 'XTickLabel', [10:-2:0 2:2:10])
EDIT
Another option:
x = linspace(0,10,20);
y = x ;
y1 = x;
plot(x,y)
hold on
plot(-x,y1)
hold off
xt = get(gca, 'XTick');
lxt = length(xt);
set(gca, 'XTickLabel', [-(xt(1:fix(lxt/2))) xt(fix(lxt/2)+1:end)])
The second option is robust to the values of ‘xt’.
Ano
Ano am 11 Dez. 2017
thank you very much for your reply! the second option outputted the x-axis without any scale. I wanted to know how can I keep the same figure with all the specifications but to plot y1= x and not y1 = -x , any suggestions?
I have no idea what you want to do.
Try this:
x = linspace(-10,10,21);
y = abs(x);
plot(x,y)
xt = get(gca, 'XTick');
lxt = length(xt);
set(gca, 'XTickLabel', [-(xt(1:fix(lxt/2))) xt(fix(lxt/2)+1:end)])
Ano
Ano am 11 Dez. 2017
thanks it works ! the general idea that I would like to learn how to do is how can I divide my figure (subplot is not what I mean here) so that I will get from the center to the Left plot var1 , and from the center to the right plot var2 , is it clear now to you what I what to do ?!
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.
Ano
Ano am 11 Dez. 2017
thank you very much !
Star Strider
Star Strider am 11 Dez. 2017
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by