Tight subplot with colorbars and subplot's 3rd parameter in Matlab?

I would like to have a tight subplot i.e. minimum spacing between figures in the subplot where

I have profiled the most popular tight subplots in FileExchange of Matlab. None (etc most popular here Pekka's version) can pass the following code

data=randi(513,513); 

ax1=subplot(2,1,1); 
plot(mat2gray(pdist(data, 'correlation')));
cbar1=colorbar(ax1);
axis(ax1, 'square');
xlim([0 size(mat2gray(pdist(data, 'correlation')),2)]);
set(cbar1, 'Visible', 'off')

ax2=subplot(2,1,2); 
imshow(squareform( mat2gray(pdist(data, 'correlation')), 'tomatrix') ); 
colormap('parula'); colorbar;
axis(ax2, 'square');  

Pekka's tight_subplot requires the syntax without the third parameter. It also fails with colorbars as in the example. I do not understand why.

Hypothesis about the 2nd problem with colorbars

I think the problem can be the fact that colorbar objects are children of the figure, not axis, and their position is defined in normalized figure units; like for annotated objects as discussed here. However, I am unsure how to adjust the tight subplot for this.

Test output after Author's edit in tight_subplot in FileExchange 3.3.2016

Code

data = randi(513, 513); 

ax1=tight_subplot(2,1,[.01 .03],[.1 .01],[.01 .01]); 
plot(mat2gray(pdist(data, 'correlation')));

ax2=tight_subplot(2,1,[.01 .03],[.1 .01],[.01 .01]); 
imshow(squareform( mat2gray(pdist(data, 'correlation')), 'tomatrix') ); 

You get

enter image description here

where the plot fails and there is noisy part in the second figure for some reason. Why?

Extension of Suever's answer to 2x2 figures

ax1=axes('OuterPosition', [0 0.5 0.5 0.5]);
plot(u, 'Parent', ax1);
set(ax1, 'XLim', [0, size(u,1)]);
cbar1 = colorbar(); % not needed to assign ax1
set(cbar1, 'Visible', 'off')

ax3 = axes('OuterPosition', [0 0 0.5 0.5]);
image(data, 'Parent', ax3);

D=mat2gray(pdist(pTFD, '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');
xlim([0 size(D,2)]);
set(cbar2, 'Visible', 'off')

ax4 = axes('OuterPosition', [0.51 0 0.5 0.5]);
imshow( D_square ); 
axis(ax4, 'square');  

where 2x2 figure system and where I think equivalent


How can you use Matlab's tight subplot with colorbars and third parameter?

Upvotes: 0

Views: 1722

Answers (2)

user6013409
user6013409

Reputation: 21

The third parameter of tight_subplot defines the gaps between axis objects. For the built-in subplot command, the third parameter defines which axis is set as the CurrentAxes of the Figure. This option is not available in tight_subplot because I personally did not find it useful. Typically, I use the returned axes handles to specify where to add graphics.

Existing axes objects are repositioned when you add a colorbar.

I have added a second output argument to tight_subplot which provides the output position of the axes so that you can "reset" the axes positions after adding a colorbar.

[hax, position] = tight_subplot();

% Add a colorbar which alters the positions
colorbar();

% Now reset the positions back to where they were
set(hax, {'Position'}, pos);

Upvotes: 2

Suever
Suever

Reputation: 65470

Rather than trying to deal with subplot and different versions on the file exchange, I would probably just manually set the positions of my axes objects to get the effect that you want. You can use normalized units so that the positions scale as the size of the parent figure changes.

Also, you can set the OuterPosition property of the axes which takes into account the room needed to properly display all text labels of the axes.

figure

data=randi(513,513);

set(0, 'defaultaxeslooseinset', [0 0 0 0])

D = mat2gray(pdist(data, 'correlation'));
square = squareform(D, 'tomatrix');

% Set normalized outer position (x,y,width,height)
ax1 = axes('OuterPosition', [0, 0.5, 1, 0.5]);
plot(D, 'Parent', ax1);
set(ax1, 'XLim', [0, size(square, 1)])
axis(ax1, 'square');

cbar1 = colorbar();
set(cbar1, 'Visible', 'off')

% Set normalized outer position (x,y,width,height)
ax2 = axes('OuterPosition', [0 0 1 0.5]);
imshow(square);
colormap('parula'); colorbar;
axis(ax2, 'square');

enter image description here

And if you remove the x and y ticks on the axes

set([ax1,ax2], 'xtick', [], 'ytick', []);

enter image description here

This can easily be adapted to any dimensions with something similar to the following

figure;
% [Rows, Columns]
axdim = [3, 3];

width = 1 ./ axdim(2);
height = 1./ axdim(1);

[x,y] = meshgrid(linspace(0,1,axdim(2)+1), ...
                 linspace(0,1, axdim(1)+1));

for k = 1:numel(x)
    ax = axes('OuterPosition', [x(k), y(k), width, height]);
    set(ax, 'xtick', [], 'ytick', []);
end

Upvotes: 1

Related Questions