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

조회 수: 32 (최근 30일)
Matthias
Matthias 2013년 6월 16일
답변: Achille Joliot 2022년 3월 25일
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