set(0, 'DefaultAxesBox', 'off') is not honored by plot()!?

25 views (last 30 days)
Hello,
I am trying to set up Matlab such that plots look presentable by default.
For this, I want to switch off the mirrored axes that Matlab draws at the top and right edges of plots. The command for this is:
set(gca, 'box', 'off')
Unfortunately, plot() ignores the default setting of this property:
>> set(0, 'DefaultAxesBox', 'off');
>> plot(rand(100,1))
>> get(gca, 'Box')
ans =
on
How can I make plot() honor the default setting, i.e. without having to set this manually every time I use plot()?
Thanks for your help!
PS: The same is true for some other settings, such as line color. I am interested in a general solution, of course.

Accepted Answer

Wayne King
Wayne King on 16 Jun 2013
Edited: Wayne King on 16 Jun 2013
plot() modifies some of the axes properties, 'Box' is one of those. The simplest thing to do is just define a new function that calls plot. In that function, you can control what plot() does and then you're still just using one line to call the function
function myplot(data)
plot(data)
set(gca,'Box','off')
end
>> data = randn(1000,1);
>> myplot(data)
  2 Comments
Duijnhouwer
Duijnhouwer on 23 Mar 2018
Edited: Duijnhouwer on 23 Mar 2018
function varargout = plot(varargin)
% Override builtin plot and honors the following settings of the
% graphics root that builtin plot for some reason ignores:
% 'DefaultAxesBox'
% 'DefaultAxesTickDir'
h=builtin('plot',varargin{:});
set(get(h,'Parent'),'Box',get(groot,'DefaultAxesBox'));
set(get(h,'Parent'),'TickDir',get(groot,'DefaultAxesTickDir'));
varargout{1}=h;
end
Harry Dymond
Harry Dymond on 3 Jun 2021
Edited: Harry Dymond on 3 Jun 2021
Thanks for the code @Duijnhouwer! Here's a couple of enhancements*, to handle situations where the inputs are matrices, and/or the axes box settings have been explicitly changed from the default and the axes have "hold" on:
function varargout = plot(varargin)
plotH = builtin('plot',varargin{:});
axH = ancestor(plotH(1),'Axes'); % plotH can be an array if inputs to 'plot' are matrices
if ~ishold(axH), axH.Box = get(groot,'DefaultAxesBox'); end
if nargout, varargout{1} = plotH; end
end
If you overload the builtin "plot", MATLAB will issue warnings. You can turn those off using:
warning('off','MATLAB:dispatcher:nameConflict')
*you can make the builtin plot honour the TickDir default using:
set(groot,'DefaultAxesTickDirMode','manual');

Sign in to comment.

More Answers (2)

Jan
Jan on 16 Jun 2013
The root properties 'factoryAxesBox' and 'defaultAxesBox' are both set to 'off' in the default setup already. Therefore this does not draw the box:
line(1:10, rand(1, 10));
But plot is a high-level command, which modifies several properties of the axes autonomously, e.g. it clear the 'tag':
AxesH = axes('Tag', 'hello');
plot(AxesH, 1:10);
get(AxesH, 'Tag'); % >> '' !!!
And obviously the 'Box' property is also dominated by the plot command, and not by the root settings.
  1 Comment
Will Adler
Will Adler on 2 Oct 2014
So how would we go about changing this default behavior, if we never want box to appear?

Sign in to comment.


Achille Joliot
Achille Joliot on 25 Mar 2022
Hi, i avoid the problem by using hold on before plotting.
set(0, 'DefaultAxesBox', 'off');
hold on
plot(linspace(1,10,50))
It also works if you run the set function in another file and you just execute
hold on
plot(linspace(1,10,50))
Without even declaring any axis or figure.

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!