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

眉间皱痕 提交于 2019-12-19 11:46:22

问题


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

  • you have subplot's 3rd parameter i.e. you can decide where the picture is going to be i.e. easy to move between subplot and new_tight_subplot, and
  • you can use it with colorbars.

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

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

  • xlim([0 size(D,2)]); is same as set(ax1, 'XLim', [0, size(D,2)]);. Right?
  • ...

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


回答1:


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');

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

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

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



回答2:


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);


来源:https://stackoverflow.com/questions/35770961/tight-subplot-with-colorbars-and-subplots-3rd-parameter-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!