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

31 Ansichten (letzte 30 Tage)
Matthias
Matthias am 16 Jun. 2013
Beantwortet: Achille Joliot am 25 Mär. 2022
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.

Akzeptierte Antwort

Wayne King
Wayne King am 16 Jun. 2013
Bearbeitet: Wayne King am 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 Kommentare
Duijnhouwer
Duijnhouwer am 23 Mär. 2018
Bearbeitet: Duijnhouwer am 23 Mär. 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 am 3 Jun. 2021
Bearbeitet: Harry Dymond am 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');

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jan
Jan am 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 Kommentar
Will Adler
Will Adler am 2 Okt. 2014
So how would we go about changing this default behavior, if we never want box to appear?

Melden Sie sich an, um zu kommentieren.


Achille Joliot
Achille Joliot am 25 Mär. 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.

Kategorien

Mehr zu Visual Exploration finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by