image thumbnail

Draw or add axes to figure

by

 

18 Jun 2013 (Updated )

Super helpful way to visualize axes in a 2D figure.

drawAxes(ax)
%% drawAxes 
% DRAWAXES will draw lines representing the horizontal and vertical axes
% in a two-dimensional plot on the axis identified by the input handle
% |ax|.  If no input is provided, it uses the current figure's current
% axes.
%
% Example syntax:
%
%    >> drawAxes()
%    >> drawAxes(ax)
% 
% Copyright 2013 The MathWorks, Inc.
function drawAxes(ax)

  if ~nargin
    ax = axis(gca);
  end

  boundsZero = prod(reshape(ax,2,2)) < 0;

  if boundsZero(1) % if x limit contains zero
      line( [0,0], [ax(3),ax(4)],...
          'LineStyle','--',...
          'LineWidth',1,...
          'Color','k')
  end

  if boundsZero(2) % if y limit contains zero
      line( [ax(1),ax(2)], [0,0],...
          'LineStyle','--',...
          'LineWidth',1,...
          'Color','k' )
  end

end

Contact us