删除子图中的死区,同时保留标题&标签 [英] Removing deadspace in subplots while retaining title & labels

查看:107
本文介绍了删除子图中的死区,同时保留标题&标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我在matlab中遇到问题
我想创建一个包含10个子图的数字。在2X5方向。但问题是我想减少它们之间的死区空间。我还需要保留标题,xlabel为第一行图像&第二行图像的xlabel(仅)。不需要ylabels。

Hi I have a problem in matlab I want to create a figure containing 10 subplots. in 2X5 orientation. But the problem is that I want to reduce the deadspace area between them. I also require to keep the title, xlabel for the first row of images & the xlabel(only) for the second row of images. No ylabels are required.

直到现在我已经完成了这个代码(第一行):一旦我想到第一行,第二行应该是小菜一碟。

Till now I have done this code (for the first row): Once I figure the first row , the second row should be a piece of cake.

close all

figure,set(gca,'Color','none')
subplot(2,5,1);subplot('Position',[0.02 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,1)), [0 0], 'r','LineWidth',2);
hold off;title('1st curve','FontSize',12);xlabel('(a)','FontSize',12)
subplot(2,5,2);subplot('Position',[0.215 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,2)), [0 0], 'r','LineWidth',2);
hold off;title('2nd curve','FontSize',12);xlabel('(b)','FontSize',12)
subplot(2,5,3);subplot('Position',[0.410 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,3)), [0 0], 'r','LineWidth',2);
hold off;title('3rd curve','FontSize',12);xlabel('(c)','FontSize',12)
subplot(2,5,4);subplot('Position',[0.605 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,4)), [0 0], 'r','LineWidth',2);
hold off;title('4th curve','FontSize',12);xlabel('(d)','FontSize',12)
subplot(2,5,5);subplot('Position',[0.8 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,5)), [0 0], 'r','LineWidth',2);
hold off;title('5th curve','FontSize',12);xlabel('(e)','FontSize',12)

这给我一个这样的输出:

This gives me an output like this :

如您所见,第四张图像不存在。代码有什么问题?我已经多次重新检查我的代码和计算,但找不到任何错误。

As you can see the fourth image is not present. What is wrong with the code ? I have rechecked my code several times and the computations too but could not find any error.

有趣的是,当我运行此代码[只有第4个子图]时,

Interestingly when I run this code [only having the 4th subplot]

subplot(2,5,4);subplot('Position',[0.605 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,4)), [0 0], 'r','LineWidth',2);
hold off;title('4th curve','FontSize',12);xlabel('(d)','FontSize',12)

我得到了这个答案!!

I get this answer !!

请谁能告诉我我错在哪里?注意我确实需要xlabel和第一行图像的标题。

Please can anyone tell me where am I wrong ? Note I do require the xlabel and the title for the first row of the images.

编辑:然后我去做了这件事。现在我得到五张图片。然而,在所有情况下图像之间的间隔不均匀,特别是第一和第二之间的间隔。第二张图片与其他图片不同。

I then went and did this thing. Now I get the five images. However the spacing between the images in all the cases are not uniform, specifically spacing between 1st & 2nd image is different from the rest.

我的代码:

close all

figure,set(gca,'Color','none')
subplot(2,5,1);subplot('Position',[0.02 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,1)), [0 0], 'r','LineWidth',2);
hold off;title('1st curve','FontSize',12);xlabel('(a)','FontSize',12)
subplot(2,5,2);subplot('Position',[0.210 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,2)), [0 0], 'r','LineWidth',2);
hold off;title('2nd curve','FontSize',12);xlabel('(b)','FontSize',12)
subplot(2,5,3);subplot('Position',[0.405 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,3)), [0 0], 'r','LineWidth',2);
hold off;title('3rd curve','FontSize',12);xlabel('(c)','FontSize',12)
subplot(2,5,4);subplot('Position',[0.600 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,4)), [0 0], 'r','LineWidth',2);
hold off;title('4th curve','FontSize',12);xlabel('(d)','FontSize',12)
subplot(2,5,5);subplot('Position',[0.795 0.51  0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,5)), [0 0], 'r','LineWidth',2);
hold off;title('5th curve','FontSize',12);xlabel('(e)','FontSize',12)

现在我得到了这个答案:

Now I get this answer :

我该如何纠正这个?请帮助!在此先感谢!!

How can I correct this? Pleae help! Thanks in advance!!

编辑:我似乎已经解决了这个问题。请看下面的答案。不过我确实有查询。
我想让代码在循环中运行,因此实现了这段代码。

EDIT : I seems to have solved this problem. Please seem my answer below. However I do have query. I wanted to make the code run in loops and so implemented this code .

I = imread('coins.png');
I = imresize(I,[128 128]);
for no = 1:5
    subplot('Position',[0.025+0.19*(no-1) 0.51 0.19 0.48]);
    imshow(I);
    get(gca,'Position');set(gca, 'XTick', []);set(gca, 'YTick', []);
    title({'Image' num2str(no)});
    subplot('Position',[0.025+0.19*(no-1) 0.03  0.19 0.48]);
     imshow(I);
    get(gca,'Position');set(gca, 'XTick', []);set(gca, 'YTick', []);
    title({'Image' num2str(no)});
end

答案是这个

The answer comes out as this

此代码可以进行矢量化吗?

Can this code be vectorised ??

推荐答案

内置的子图功能非常强大而且很棒。我认为最好坚持 subplot ,而不是使用自定义计算位置。问题当然是 subplot 坚持额外空间。这个空间受两个因素控制。第一个是数字应用程序数据的用户控制的 SubplotDefaultAxesLocation 属性。第二个是子图函数中的硬编码插入 SubplotDefaultAxesLocation 属性控制图的边缘周围的空白,而插入控制面板之间的空间。您可以删除图周围的空间

The built in subplot function is really powerful and nice. Instead of going with custom computed positions, I think it is best to stick with subplot. The problem of course is that subplot sticks in "extra" space. This space is controlled by two factors. The first is the user controlled SubplotDefaultAxesLocation property of the application data of figures. The second is a hardcoded inset within the subplot function. The SubplotDefaultAxesLocation property controls the whitespace around the edge of the figure while the inset controls the space between the panels. You can remove the space around the figure with

hfig = figure;
setappdata(hfig, 'SubplotDefaultAxesLocation', [0, 0, 1, 1])

要删除需要编辑(或复制编辑)的面板之间的空间 subplot.m 在R2013a中,有问题的行是129和130

To remove the space between panels you need to edit (or copy edit) subplot.m In R2013a the offending lines are 129 and 130

% This is the percent offset from the subplot grid of the plotbox.
inset = [.2, .18, .04, .1]; % [left bottom right top]

如果您将第130行更改为

If you change line 130 to be

inset = [0.0, 0.0, 0.0, 0.0]; % [left bottom right top]

然后面板之间没有任何空格。更好的是类似

then the panels will not have any space between them. Even better would be something like

% This is the percent offset from the subplot grid of the plotbox.
if ~isappdata(ancestorFigure, 'SubplotDefaultInset')
    inset = [.2, .18, .04, .1]; % [left bottom right top]
else
    inset = getappdata(ancestorFigure, 'SubplotDefaultInset');
end

所以你可以控制插入。使用这个略微修改的子图函数

so you can control the inset. With this slightly modified subplot function

I = imread('coins.png');
I = imresize(I,[128 128]);

voffset = 0.3;
hfig = figure;
setappdata(hfig, 'SubplotDefaultAxesLocation', [0, voffset/2, 1, 1-voffset]);
setappdata(hfig, 'SubplotDefaultInset', [0, 0, 0, 0]);

nrow = 2;
ncol = 5;
hax = nan(nrow, ncol);
for irow = 1:nrow
    for icol = 1:ncol
        hax(irow, icol) = mysubplot(nrow, ncol, icol+(irow-1)*ncol, 'align');
        imshow(I);
        if irow == 1
            title(hax(irow, icol), ['Curve #', num2str(icol)], 'FontSize',12);
        end
        if irow == nrow
            xlabel(hax(irow, icol), ['(', char(96+icol), ')'], 'FontSize',12);
        end
    end
end
set(hax,  'XTick', [], 'YTick', []);

pos = get(hfig, 'Position');
set(hfig, 'Position', [pos(1:2), (1-voffset)*pos(3), nrow/ncol*pos(3)]);

您需要 voffset 才能允许标题和xlabel以适应人物。您需要缩放图形,使其具有正确的宽高比来处理面板。结果是

You need the voffset to allow the title and xlabel to fit on the figure. You need to scale the figure so that it has the correct aspect ratio to handle the panels. The result is

曲线1和曲线2以及曲线3和曲线4之间的小差距似乎是一个渲染假象。

The small gap between curve 1 and 2 and curve 3 and 4 appears to be a rendering artefact.

如果你想要行之间的标签,你需要添加一些额外的空间。例如,只需在x轴上添加标签即可

If you want the labels between the rows, you need to add some extra space. For example to add just labels on the x axis you can do

voffset = 0.35;
setappdata(hfig, 'SubplotDefaultAxesLocation', [0, voffset/4, 1, 1-voffset/2]);
setappdata(hfig, 'SubplotDefaultInset', [0, voffset/2, 0, 0]);

这篇关于删除子图中的死区,同时保留标题&标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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