Removing dead space in subheadings while maintaining titles and tags

Hi, I have a problem in matlab. I want to create a number containing 10 subplots. in orientation 2X5. But the problem is that I want to reduce the area of ​​dead space between them. I also need to keep the title, xlabel for the first line of images and xlabel (only) for the second line of images. No yards required.

So far I have done this code (for the first line): as soon as I draw the first line, the second line 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 this conclusion:

enter image description here

As you can see, the fourth image is missing. What is wrong with the code? I rechecked my code and calculations several times, too, but could not find the error.

I wonder when I run this code [with only the 4th subtitle]

 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 !! enter image description here

Please tell me where am I wrong? Note. I need xlabel and a title for the first line of images.

EDIT: I then went and did it. Now I get five images. However, the interval between images in all cases is uneven, in particular, the distance between the 1st and 2nd images differs from the others.

My code is:

 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: enter image description here

How can i fix this? Help! Thanks in advance!

EDIT: It seems I solved this problem. Please show my answer below. However, I have a request. I wanted to make the code run in a loop and thus implement 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 goes like this enter image description here

Can this code be vectorized ??

+4
source share
5 answers

The built-in subplot function is really powerful and enjoyable. Instead of going with custom calculated positions, I think it's best to stick with subplot . The problem, of course, is that subplot adheres to "extra" space. This space is driven by two factors. The first is the user property SubplotDefaultAxesLocation data application data. The second is the hard-coded inset in the subplot function. The SubplotDefaultAxesLocation property controls the space at the edge of the figure, and inset controls the space between the panels. You can remove the space around the shape with

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

To remove the space between the panels, you need to edit (or copy the edit) subplot.m In R2013a, the offensive 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] 

If you change line 130 to

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

then the panels will not have a 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)]); 

You will need a voffset so that the header and xlabel can fit in the picture. You need to scale the shape so that it has the correct aspect ratio for panel processing. The result is

enter image description here

A small gap between curves 1 and 2 and curves 3 and 4 seems to be a rendering artifact.

If you need labels between the lines, you need to add extra space. For example, to add only 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]); 
+3
source

The subaxis function can be found here in Matlab File Exchange: sublisis from Aslak Grinsted offers you a very convenient solution.

+2
source

when the subheadings overlap, the previous one is hidden.

try reducing the position width a bit.

they should appear again

there may also be some problems with snapping to the grid, how is this manifested when the window is resized?

+2
source
 close all figure,set(gca,'Color','none') subplot('Position',[0.025 0.51 0.19 0.48]);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);h=xlabel('(a)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); subplot('Position',[0.025+0.19*1 0.51 0.19 0.48]);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);h=xlabel('(b)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); subplot('Position',[0.025+0.19*2 0.51 0.19 0.48]);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);h=xlabel('(c)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); subplot('Position',[0.025+0.19*3 0.51 0.19 0.48]);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);h=xlabel('(d)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); subplot('Position',[0.025+0.19*4 0.51 0.19 0.48]);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);h=xlabel('(e)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); subplot('Position',[0.025 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []); seg = phi0(:,:,1)<=0;imshow(seg); h=xlabel('(f)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); subplot('Position',[0.025+0.19*1 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []); seg = phi0(:,:,2)<=0;imshow(seg); h=xlabel('(g)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); subplot('Position',[0.025+0.19*2 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []); seg = phi0(:,:,3)<=0;imshow(seg); h=xlabel('(h)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); subplot('Position',[0.025+0.19*3 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []); seg = phi0(:,:,4)<=0;imshow(seg); h=xlabel('(i)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); subplot('Position',[0.025+0.19*4 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []); seg = phi0(:,:,5)<=0;imshow(seg); h=xlabel('(j)','FontSize',12); s=get(h,'Position'); s(2)=s(2)-28; set(h,'Position',s); 

Thanks to everyone that I received the correct answer. I changed my code a bit.

This was the answer I received:

enter image description here

+1
source

Of course, this can be done with one loop (using an array of cells for your variables and one for the graphing functions), but for simplicity I would just split it into two loops.

 labels = {'(a)' , '(n)' , '(etc)' , 'asdf','asddf'} jay=1 for aye =1:5 subplot('Position',[0.025+0.19*(aye-1) 0.51-0.48*(jay-1) 0.19 0.48]); ... h=xlabel(labelsabc{aye},'FontSize',12); .... end 
+1
source

Source: https://habr.com/ru/post/982177/


All Articles