How to plot variable values on x axis?

조회 수: 8 (최근 30일)
Cristina Magda
Cristina Magda 2017년 8월 18일
댓글: Adam 2017년 8월 18일
Ok, so I didn't knew how exactly ask in one sentence what I need. I have Y data that is like: y=[0:1:positive-value positivevalue-1:-1:0 0:-1:negative-value negative-value+1:1:0] Thus resulting in a range of values. The X data is function of Y, thus resulting in same number of values and same character increasing and decreasing. A little example (I have more values then that):
y=[0 1 2 3 4 3 2 1 0 0 -1 -2 -3 -4 -3 -2 -1 0]
X=[0.2 1 6 9 12 9 6 1 0.2 0.2 -1 -6 -9 -12 -9 -6 -1 0.2]
Now I need to plot x and y. I could use line(x,y) or plot(x,y). But when I do that the resulting graph is a line (see graph below), because x data falls back on the previous values. I need that X data to be raising from 0 to max value and then back to 0, then from 0 to min value and back to 0. Thus the Y data plotting over X would be a jigsaw or something like that. Or we could say I would need a graph looking like x=sin(y) (like in second figure, only x would have different values getting from 0 to max and back to 0). Is that even possible? Thank you.
  댓글 수: 2
Adam
Adam 2017년 8월 18일
You should be able to just plot it as a new line (or plot) each time there is a change of direction. A bit fiddly, but simple enough to locate those change points.
Cristina Magda
Cristina Magda 2017년 8월 18일
I thought of that, but didn't knew how to do that automatically. Besides, every time it plots a new line it would start from the (0,0)point at the beginning and that's not how I want it. Or don't know how to to different.

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2017년 8월 18일
The easiest way:
y=[0 1 2 3 4 3 2 1 0 0 -1 -2 -3 -4 -3 -2 -1 0];
x=[0.2 1 6 9 12 9 6 1 0.2 0.2 -1 -6 -9 -12 -9 -6 -1 0.2];
z = 1:numel(x);
figure(1)
plot3(z, x, y)
grid on
view(0,0)
  댓글 수: 2
Cristina Magda
Cristina Magda 2017년 8월 18일
편집: Cristina Magda 2017년 8월 18일
Works with plot(z,y) also. As I read about command numel - it counts all the values from that array. Then from 1 to that count, I get a number for each value, 1 2 3 4 ...And then plots the x number (1,2,3...) with value from Y. Tick labels now show 10,20,30...etc, but I don't want that. I want to show the maximum and minimum from x, like 0.2 then 12 then 0.2 again then -12 and so on. I have 132 values, need to show the min, max,0 and some points between. Doing that manually is out of the question.
Picture of what I've obtained.
Adam
Adam 2017년 8월 18일
You can change the Tick Labels on the x axis, converting your x array (or a subset of it) to strings and then change the ticks to the same indices into z.
e.g. if you want the 1st, 5th, 9th, 12th values as your ticks, then you can just edit the
XTick
property to be z( [1 5 9 12] )
and
XTickLabel
to be
arrayfun( @num2str, x( [1 5 9 12] ), 'UniformOutput', false )

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by