offsetAxes.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. function offsetAxes(ax)
  2. % thanks to Pierre Morel, undocumented Matlab
  3. % and https://stackoverflow.com/questions/38255048/separating-axes-from-plot-area-in-matlab
  4. %
  5. % by Anne Urai, 2016
  6. % Modified Luis Ramos T. 2019
  7. if ~exist('ax', 'var'), ax = gca; end
  8. % If there is only one tick do not change stuff.
  9. % Otherwise modify the x and y limits to below the data (by a small amount)
  10. if numel(ax.XTick) == 1 && numel(ax.YTick) == 1
  11. return
  12. end
  13. if numel(ax.XTick) > 1
  14. shiftXAx = (ax.XTick(2) - ax.XTick(1)) / 4;
  15. ax.XLim = ax.XLim + [-1 1] * shiftXAx;
  16. end
  17. if numel(ax.YTick) > 1
  18. shiftYAx = (ax.YTick(2) - ax.YTick(1)) / 4;
  19. ax.YLim = ax.YLim + [-1 1] * shiftYAx;
  20. end
  21. % this will keep the changes constant even when resizing axes
  22. switch ax.Type
  23. case 'colorbar'
  24. return
  25. otherwise
  26. addlistener (ax, 'MarkedClean', @(obj,event)resetVertex(ax));
  27. end
  28. end
  29. function resetVertex ( ax )
  30. % extract the x axis vertext data
  31. % X, Y and Z row of the start and end of the individual axle.
  32. ax.XRuler.Axle.VertexData(1,1) = min(get(ax, 'Xtick'));
  33. % repeat for Y (set 2nd row)
  34. ax.YRuler.Axle.VertexData(2,1) = min(get(ax, 'Ytick'));
  35. % Limit last tick
  36. % X, Y and Z row of the start and end of the individual axle.
  37. ax.XRuler.Axle.VertexData(1,2) = max(get(ax, 'Xtick'));
  38. % repeat for Y (set 2nd row)
  39. ax.YRuler.Axle.VertexData(2,2) = max(get(ax, 'Ytick'));
  40. end