MATLAB Answers

How do I change the position of the axes permanently?

3 views (last 30 days)
Bassel Fadlallah
Bassel Fadlallah on 5 Jun 2021
Answered: Adam Danz on 5 Jun 2021
Although I keep putting them on origin, the X and Y axes keep going down and left every time I plot a function. So I was wondering if I can change this permanently to have them always on the origin.
  1 Comment
Jan
Jan on 5 Jun 2021
What does "keep putting them on origin" mean? What is going to where? Which origin do you mean?
Can you post some code and a screen shot, which explains, what you want to achieve?

Sign in to comment.

Answers (4)

Star Strider
Star Strider on 5 Jun 2021
This is a bit ambiguous.
One option is to use the axis function.
Another are to set the Axes Properties, specifically XAxisLocation and YAxisLocation.
.

John D'Errico
John D'Errico on 5 Jun 2021
Edited: John D'Errico on 5 Jun 2021
There are some defaults you can set as defaults, but XAxisLocation and YAxisLocation do not appear to fall under the list of things I can set as defaults for the root. I assume that is because they are axis properties, not figure properties.
However, nothing stops you from writting a little helper function, thus...
function setAxesToOrigin
set(gca,'XAxisLocation','origin')
set(gca,'YAxisLocation','origin')
end
Then just execute that function after your plot is done.

dpb
dpb on 5 Jun 2021
Taking a stab at the OP's meaning (thanks to the Crystal Ball having just been returned from the shop (yet) again...)
Try
set(groot,'defaultAxesXAxisLocation','origin')
set(groot,'defaultAxesYAxisLocation','origin')

Adam Danz
Adam Danz on 5 Jun 2021
You have to either hold the axes after setting the axis location or set the axis location after plotting.
ax = subplot(1,2,1);
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
xlim([-1,1])
ylim([-1,1])
hold on % <--------- hold axes
plot(rand(1,20)*2-1,rand(1,20)*2-1,'o')
or
ax = subplot(1,2,2);
plot(rand(1,20)*2-1,rand(1,20)*2-1,'o')
xlim([-1,1])
ylim([-1,1])
box off
ax.XAxisLocation = 'origin'; % <----- after plotting
ax.YAxisLocation = 'origin'; % <----- after plotting

Community Treasure Hunt

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

Start Hunting!