Skip to content

Instantly share code, notes, and snippets.

@numpde
Last active November 9, 2017 03:58
Show Gist options
  • Save numpde/b81b5a83ad036a0dd9fb1c92bcaeba2b to your computer and use it in GitHub Desktop.
Save numpde/b81b5a83ad036a0dd9fb1c92bcaeba2b to your computer and use it in GitHub Desktop.
Try to make a MATLAB plot a little nicer
function h = renice(h)
% function h = renice(h)
%
% h is a handle returned by PLOT and such
%
% RA, Apr 2008 -- Aug 2016
%
% License: CC-BY-4.0
set(0, 'DefaultTextInterpreter', 'tex');
set(gca, 'TickLabelInterpreter', 'tex');
set(gca, 'FontUnits', 'Normalized');
set(gca, 'FontSize', 0.04);
grid on;
set(gca, 'MinorGridLineStyle', 'none');
if (~isgraphics(h))
elseif (isgraphics(h, 'line'))
set(h, 'LineWidth', 3);
set(h, 'MarkerSize', 10);
elseif (isgraphics(h, 'legend'))
set(h, 'Interpreter', 'latex');
set(h, 'FontSize', round(get(h, 'FontSize') * 1.2));
end
niceaxis;
end
function niceaxis(enlarge)
% function NICEAXIS(BORDER_MARGIN)
%
% BORDER_MARGIN (optional)
%
% RA, Apr 2008
%
% License: CC-BY-4.0
if (nargin < 1); enlarge = 3; end;
axis tight;
v = axis;
if strcmp(get(gca, 'XScale'), 'linear')
v(1:2) = v(1:2) + enlarge * 0.05 * v(1:2) * [1 -1; -1 1];
else
v(1:2) = exp(log(v(1:2)) + enlarge * 0.05 * log(v(1:2)) * [1 -1; -1 1]);
end
if strcmp(get(gca, 'YScale'), 'linear')
v(3:4) = v(3:4) + enlarge * 0.05 * v(3:4) * [1 -1; -1 1];
else
%v(3) = max([v(3) eps]);
v(3:4) = exp(log(v(3:4)) + enlarge * 0.05 * log(v(3:4)) * [1 -1; -1 1]);
end
axis(v);
grid on;
set(gca, 'GridLineStyle', '-');
set(gca, 'MinorGridLineStyle', 'none');
set(gca, 'Color', [1 1 1]*0.99);
set(gca, 'YColor', [1 1 1]*0.4, 'XColor', [1 1 1]*0.4);
set(get(gca, 'XLabel'), 'Color', 'k');
set(get(gca, 'YLabel'), 'Color', 'k');
set(get(gca, 'ZLabel'), 'Color', 'k');
pbaspect([(1+sqrt(5))/2 1 1]);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment