Moving graph along x-axis
64 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to plot 3 different vectors that showcase the same function but with different starting points from the y-axis.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/967150/image.png)
Now what I wanna do is move the yellow and the red graph so they overlap with the blue one, to see wether my determined data actually is correct. I have another picture here to further show what exactly I mean:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/967155/image.png)
Now I have tried to do something similar but with the y-axis but unfortunately I couldn't make it work.
What would be a clever way to move the graphs without having to change the values of the vectors?
2 个评论
Torsten
2022-4-17
Now what I wanna do is move the yellow and the red graph so they overlap with the blue one, to see wether my determined data actually is correct.
Better work with the data, not with the graphics.
采纳的回答
Tala
2022-4-17
编辑:Tala
2022-4-18
I would subtitute the other 2 array in the main one and plot that. something lik ethis:
id1 = find(blue==yellow(1)); % the index on the blue curve that matches the first value of yellow
id2 = find(blue==red(1)); % the index on the blue curve that matches the first value of red
now use the id values to create a new array. newblue contain the values from blue, yellow and red curves, and you can just plot newblue.
newblue=blue;
newblue(id1:end)=yellow;
newblue(id2:end)=red;
7 个评论
Tala
2022-4-19
编辑:Tala
2022-4-19
You basically need to move your curves along th X axis; in other words you need to plot(x+dx,y). To define dx you need to clean up your data. in the code below, I removed all nans and chose the last value (because there is no "jump") as the ref point and moved all the curves. As you see you have a better match towards the tail of the curves because the ref point is the last point.
You could clean up your data and get rid of the jump in the start of blue curve and use the first element as your ref point, then you get a better match towards the beginning. In that case just change "end" to "1"
you could take the avarage of first and last, or even interpolate over a unique x aeeay, and move each point individually using this method
X1=data.x1;Y1=data.y1;X2=data.x2;Y2=data.y2;X3=data.x3;Y3=data.y3;
% removing nans
TF = find(~isnan(Y1));
Y1N=Y1(TF);
X1N=X1(TF);
TF = find(~isnan(Y2));
Y2N=Y2(TF);
X2N=X2(TF);
TF = find(~isnan(Y3));
Y3N=Y3(TF);
X3N=X3(TF);
% now plot arrays with no nans
plot(X1N,Y1N)
hold on
plot(X2N,Y2N)
plot(X3N,Y3N)
% defining dx and moving your plots
d13=(X1N(end)-X3N(end));
plot(X3N+d13,Y3N);
d12=(X1N(end)-X2N(end));
plot(X2N+d12,Y2N);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/969625/image.jpeg)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!