MATLAB Answers


How to change the origin from (0,0) to different coordinates in a plot

Asked by FD
on 18 Apr 2019
Latest activity Answered by Kelly Kearney
on 18 Apr 2019
I have two datasets with 1100x1 double. The rows represent different time points, while the values within each cell of the dataset represent a certain width. Therefore, there are 1100 time points. Matlab treats these data as starting from time zero. However, recording started from time -100.
When I plot these data on the same figure (with Matlab 2018a)
hold on
plot(y, '-r');
the x and y axis are automatically placed on the bottom (the x axis) and left (y axis) of the plot. I would like to move the axes to time point 0 and width 0.
I can move the x axis with:
ax.XAxisLocation = 'origin';
However, I cannot do the same for the y axis, as Matlab uses as 'origin' for the y axis the default (0,0), rather than the (100,0).
I know that I could simply draw a new line to create the new y axis with
plot([100 100], ylim, 'k-', 'LineWidth',1);
but I want to keep ticks and tick labels as they are in the default settings.
How can I move the y axis? Is thre a way to do something like this: ax = gca; ax.YAxisLocation = (ax.XLim(1)+100); %which doesn't working


Sign in to comment.




2 Answers

Answer by Hassan Nawaz on 18 Apr 2019

Hi, You can try using the axis command. With this you can adjust as per your desire how the overall posittive and negative range will look like. the syntax is as
axis([-x +x -y +y])

  1 Comment

So, in my example, which values should I be using?

Sign in to comment.

Answer by Kelly Kearney
on 18 Apr 2019

You don't need to move the axis; you need to provide x-values for your data.
Right now, you're using the ydata-only option for plot. When you do that, it assumes that the x values are 1:length(y). So
is the equivalent of
plot(1:length(x), x, '-b');
Instead, provide the real x values. In your case, that's probably something like:
t0 = -100;
dt = 0.01; % or whatever the spacing is in your points.
t = t0 + (0:length(x)-1).*dt;
plot(t, x, '-b')


Sign in to comment.