0
votes

I would like to have vertical/horizontal grid lines across spacing of figures in Matlab 2015b. I know that you can have a background image that can be a network of gridlines by background, something partial about it at How do I add a background image to my GUI or figure window? However, I think gridlines without a picture would be a better choice

enter image description here

Example code

data=randi(513,513);
D=mat2gray(pdist(data, 'correlation'));

ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
plot(D, 'Parent', ax2);
set(ax2, 'XLim', [0, size(D,1)])
axis(ax2, 'square');
title('Corr pdist');
cbar2 = colorbar(); 
set(ax2, 'XLim', [0 size(D,2)]);
set(cbar2, 'Visible', 'off')
grid minor;
% Force a draw event to have the axes determine where the
labelconverter = @(x)sprintf('%.2g', x); % https://stackoverflow.com/a/35780915/54964
callback = @(varargin)set(ax2, 'xticklabels', arrayfun(labelconverter, get(ax2, 'xtick'), 'uniform', 0));
set(hFig, 'SizeChangedFcn', callback);
callback(); % necessary for the original small window and its scientific numbering

D_square=squareform(D, 'tomatrix');
ax4 = axes('OuterPosition', [0.51 0 0.5 0.5]);

imshow( D_square ); 
colormap('parula'); colorbar;
axis(ax4, 'square');  
title('Square Corr pdist');

where the system is based on relative positions, rather than subplot as described in the answer here about Tight subplot with colorbars and subplot's 3rd parameter in Matlab?

Hypothetical methods

  • have master figure which has grid lines; embed those two figures there as parent positions; not sure if possible
  • assign gridlines function to the function background
  • have a separate background image which has static gridlines

Function about Suever's script

function [ hFig ] = init_background_grid(thickness)

    %% Background
    hFig=figure;
    backax = axes('Parent', hFig);

    % Ensure that this is below other objects
    uistack(backax, 'bottom');

    % Span the whole figure
    set(backax, 'Position', [0 0 1 1]);

    grid(backax, 'on')

    % Make it invisible except for the grid and
    % ensure it isn't able to be interacted with
    set(backax, 'HitTest', 'off', ...
            'HandleVisibility', 'off', ...
            'GridLineStyle', '-', ...
            'Color', 'none', ...
            'XColor', 'none', ...
            'YColor', 'none')

    % Determine grid spacing with x/y ticks
    % Increase nLines for a finer grid
    nLines = thickness; % 30 default

    set(backax, 'XTick', linspace(0, 1, nLines), ...
            'YTick', linspace(0, 1, nLines));

end

How can you have grid lines in the background i.e. in the spacing between figures?

1

1 Answers

2
votes

I would probably just setup an axes that spans your entire figure, then turn on the grid lines, then set the color of the axes to 'none'. A full example is shown below.

fig = figure();
backax = axes('Parent', fig);

% Ensure that this is below other objects
uistack(backax, 'bottom');

% Span the whole figure
set(backax, 'Position', [0 0 1 1]);

grid(backax, 'on')

% Make it invisible except for the grid and
% ensure it isn't able to be interacted with
set(backax, 'HitTest', 'off', ...
            'HandleVisibility', 'off', ...
            'GridLineStyle', '-', ...
            'Color', 'none', ...
            'XColor', 'none', ...
            'YColor', 'none')

% Determine grid spacing with x/y ticks
% Increase nLines for a finer grid
nLines = 20;

set(backax, 'XTick', linspace(0, 1, nLines), ...
            'YTick', linspace(0, 1, nLines));

enter image description here

You can then add any plots, uicontrols, etc. on top of this and it should work just fine. It will also handle the case where you decide that you want a different figure color.