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

31 次查看(过去 30 天)
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.

采纳的回答

Wayne King
Wayne King 2013-6-16
编辑:Wayne King 2013-6-16
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 个评论
Duijnhouwer
Duijnhouwer 2018-3-23
编辑:Duijnhouwer 2018-3-23
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 2021-6-3
编辑:Harry Dymond 2021-6-3
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');

请先登录,再进行评论。

更多回答(2 个)

Jan
Jan 2013-6-16
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 个评论
Will Adler
Will Adler 2014-10-2
So how would we go about changing this default behavior, if we never want box to appear?

请先登录,再进行评论。


Achille Joliot
Achille Joliot 2022-3-25
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.

类别

Help CenterFile Exchange 中查找有关 Visual Exploration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by