image - 在保留标题和标签的同时删除子图中的死角

标签 image matlab plot subplot

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

到目前为止,我已经完成了这段代码(第一行):一旦我计算出第一行,第二行应该是小菜一碟。

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)

这给了我这样的输出:

enter image description here

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

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

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)

我得到了这个答案! enter image description here

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

编辑:然后我去做了这件事。现在我得到了五张图片。然而,所有情况下图像之间的间距并不统一,特别是第 1 和第 2 个图像之间的间距与其余图像不同。

我的代码:

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)

现在我得到了这个答案: enter image description here

我该如何纠正这个问题?请帮忙!提前致谢!!

编辑:我似乎已经解决了这个问题。请看下面我的回答。但是我确实有疑问。 我想让代码循环运行,因此实现了这段代码。

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

答案是这样出来的 enter image description here

此代码可以向量化吗??

最佳答案

内置subplot功能真的很强大很好用。我认为最好坚持使用 subplot,而不是使用自定义计算位置。 .问题当然是subplot坚持“额外”空间。这个空间由两个因素控制。首先是用户控制SubplotDefaultAxesLocation图形应用数据的属性。第二个是硬编码的 insetsubplot 内功能。 SubplotDefaultAxesLocation属性控制图形边缘周围的空白,而 inset控制面板之间的空间。您可以使用

删除图形周围的空间
hfig = figure;
setappdata(hfig, 'SubplotDefaultAxesLocation', [0, 0, 1, 1])

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

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

如果将第 130 行更改为

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

那么面板之间将没有任何空间。更好的是像

% 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

所以你可以控制inset .稍作修改 subplot功能

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 适合图形。您需要缩放图形,使其具有正确的纵横比来处理面板。结果是

enter image description here

曲线 1 和 2 与曲线 3 和 4 之间的小间隙似乎是渲染伪像。

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

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

关于image - 在保留标题和标签的同时删除子图中的死角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18853962/

相关文章:

r - R/Python/Matlab 中带动态权重的最短路径图(重复 Dijkstra?距离矢量路由算法?)

matlab从关联矩阵绘制图形

android - 获取 ImageView 中图像的显示大小

python - 如何在屏幕上显示 PIL 图像?

java - Facebook 个人资料图像未显示在 Android ImageView 中

r - 当 "add = TRUE"时 curve() 不会将曲线添加到我的绘图中

r - 如何在 R Plotly 中映射分类变量以对 3D 散点图中的点轮廓进行着色?

image - Canvas 被 CORS 数据和 S3 污染

matlab - 频谱图及其含义

matlab - 4D "plot"的 2D 轮廓/切片?