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

32 vues (au cours des 30 derniers jours)
Matthias
Matthias le 16 Juin 2013
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.

Réponse acceptée

Wayne King
Wayne King le 16 Juin 2013
Modifié(e) : Wayne King le 16 Juin 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 commentaires
Duijnhouwer
Duijnhouwer le 23 Mar 2018
Modifié(e) : Duijnhouwer le 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 le 3 Juin 2021
Modifié(e) : Harry Dymond le 3 Juin 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');

Connectez-vous pour commenter.

Plus de réponses (2)

Jan
Jan le 16 Juin 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 commentaire
Will Adler
Will Adler le 2 Oct 2014
So how would we go about changing this default behavior, if we never want box to appear?

Connectez-vous pour commenter.


Achille Joliot
Achille Joliot le 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.

Catégories

En savoir plus sur Visual Exploration dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by