在Matlab中带有颜色条和子图的第3个参数的紧密子图? [英] Tight subplot with colorbars and subplot's 3rd parameter in Matlab?

查看:102
本文介绍了在Matlab中带有颜色条和子图的第3个参数的紧密子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个紧凑的子图,即子图中图形之间的最小间距

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

  • 您具有子图的第三个参数,即您可以决定图片的位置,即易于在subplotnew_tight_subplot之间移动,以及
  • 您可以将其与颜色条一起使用.
  • 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.

我已经介绍了Matlab的FileExchange中最受欢迎的紧密子图. 没有一个(例如最流行的此处 Pekka版本)可以通过以下代码

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的tight_subplot要求语法不带第三个参数. 如示例中所示,它也不会带有颜色条.我不明白为什么.

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.

我认为问题可能出在以下事实上:颜色条对象是图形的子级,而不是轴,并且其位置是以标准化图形单位定义的;如此处所述. 但是,我不确定如何为此调整紧的子图.

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.

代码

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

你得到

plot失败的地方,由于某种原因,第二张图中有嘈杂的部分.为什么?

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

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

2x2图形系统和我认为等效的系统

where 2x2 figure system and where I think equivalent

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

如何使用带有颜色条和第三个参数的Matlab紧密子图?

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

推荐答案

我可能不想手动处理文件交换中的subplot和不同版本,而是手动设置轴对象的位置以获得效果你要的那个.您可以使用归一化的单位,以便随着父图大小的变化而改变位置.

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.

此外,您可以设置轴的OuterPosition属性,其中要考虑正确显示轴的所有文本标签所需的空间.

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

如果您删除了轴上的x和y刻度线

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

这篇关于在Matlab中带有颜色条和子图的第3个参数的紧密子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆