Is there a way to define a default axis position, similar to defaultFigurePosition?

When plotting with the plot command, you can change the position of the axes using e.g.:
set(gca,'Units', 'normalized','Position',[0.1 0.1 0.9 0.9])
which is similar to how a figures position can be changed.
Is there a setting for the default axis positions, analog to setting a default position for figures:
set(0, 'defaultFigurePosition', [0.25 0.25 0.7 0.7])
Or is there any other to set the plot axes in a particular way by default?

 Accepted Answer

Yes.
% A strange small size to be sure, that it works:
set(groot, 'defaultAxesPosition', [0.25, 0.25, 0.2, 0.2])
figure;
plot(1:10)
This is a bad idea, because it modifies all folling axes objects. It is smarter to do this for your own figures only:
% Reset the stuff from above:
set(groot, 'defaultAxesPosition', get(groot, 'factoryAxesPosition'))
figure('defaltAxesPosition', [0.55, 0.55, 0.2, 0.2])
plot(1:10)
Using 0 as root object is outdated since 2014. Prefer groot.

4 Comments

Thank you for your answer, I agree this is not ideal as it affects all axes, is there a way to restrict it to those created by the plot command?
Also do you know if there is any documentation for this?
You find some documentation by:
docsearch factory
For all possible parameters see:
a = get(groot, 'Factory');
a = get(groot, 'Default');
Limiting the effect for axes created implicitly by plot is not possible.
Hello @Jan,
Is your solution still working for uifigure/uiaxes in Matlab 2023b? I am using the Default* properties for years, but now I am moving to uifigures and getting problems.
In the examples below the uiaxes does not work....
Working:
%% working figure/axes
hFig = figure();
set(hFig, 'DefaultAxesUnits', 'normalized')
set(hFig, 'DefaultAxesPosition', [0 0 1 1]);
set(hFig, 'DefaultAxesCreateFcn', @(~,~)disp('created'))
hAx = axes(hFig);
disp(get(hAx, 'Units'))
disp(get(hAx, 'Position'))
disp(get(hAx, 'CreateFcn'))
Not working:
%% not working uifigure/uiaxes
hFig = uifigure();
set(hFig, 'DefaultAxesUnits', 'normalized')
set(hFig, 'DefaultAxesPosition', [0 0 1 1]);
set(hFig, 'DefaultAxesCreateFcn', @(~,~)disp('created'))
hAx = uiaxes(hFig);
disp(get(hAx, 'Units'))
disp(get(hAx, 'Position'))
disp(get(hAx, 'CreateFcn'))
The units are still 'pixels', the location is [10 10 400 300] and no 'created' is displayed. BUT disp(get(hAx, 'CreateFcn')) displays correct!
What is going wrong?
Note the property 'DefaultAxesButtonDownFcn' is working for both.
Thanks in advance,
Martin

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Asked:

on 12 Feb 2021

Commented:

on 27 Mar 2024

Community Treasure Hunt

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

Start Hunting!