Remove spacing in matlab subplot
Asked Answered
S

3

11

How should i remove the empty space between these images?i need to combine all these images without any space.


bot=imread('bot.jpeg');

for i= 1:25
subplot(5,5,i),imshow(bot);
end

enter image description here

Stomy answered 8/2, 2015 at 18:50 Comment(3)
The best thing you could do is to create 1 big image created by all the small ones and use just 1 imshowSpatola
use mathworks.com/matlabcentral/fileexchange/27991-tight-subplot from the FEXThose
Possible duplicate of #18854462Phan
O
11

You need to specify axes' 'Position' property when you create them with subplot.

Also, you have to adjust figure aspect ratio to match that of the image, so that all figures fit without vertical or horizontal space.

If you show a different image in each subplot, all images should have the same aspect ratio, otherwise it's not possible for them to fit in the figure without empty spaces.

bot = imread('peppers.png');
for i= 1:25
    subplot('Position',[(mod(i-1,5))/5 1-(ceil(i/5))/5 1/5 1/5])
    imshow(bot); %// or show a different image on each subplot
end
p = get(gcf,'Position');
k = [size(bot,2) size(bot,1)]/(size(bot,2)+size(bot,1));
set(gcf,'Position',[p(1) p(2) (p(3)+p(4)).*k]) %// adjust figure x and y size

enter image description here

Outlawry answered 8/2, 2015 at 19:32 Comment(2)
Thanks luis you solved my problem. now i get it what wrong i was doing by creating different subplot.Stomy
Actually my code does create different subplots. The difference is that the 'Position' is specified for each subplotOutlawry
W
4

The most canonical way would be to take a look at this answer by bla here. This answer uses a function from the MATLAB File Exchange in order to achieve the answer. However, that requires learning a new function and playing around with the parameters.

If you want something working immediately, instead of showing each subimage in a separate grid on a plot, I would simply create a new image that stacks all of those images together:

bot_new = repmat(bot, [5 5]);
imshow(bot_new);

repmat takes a matrix and duplicates / stacks / tiles itself together for as many rows and as many columns (or in any dimension) that you want. In this case, I chose to stack the image so that there are 5 rows and 5 columns of it. We next show the stacked image together with imshow.

If we used an example image from MATLAB:

bot = imread('onion.png');

If we ran the above code that tiles the images together and showed the image, this is what we get:

enter image description here

Warrantor answered 8/2, 2015 at 19:12 Comment(3)
thanks for help but ,i want different image for different tileStomy
Please elaborate. That doesn't make sense. Do you want separate variables for each tile? That isn't the best way to do it IMHO. Also what you asked and what you just told me are completely different.Warrantor
@shivamsharma your sample shows the same image all over itHamil
P
1

I copy the answer from mathworks:

For each subplot, store its handle.

 h = subplot(2,3,1);

Then set the 'position' property of h to be anything you want.

 p = get(h, 'pos');

This is a 4-element vector [left, bottom, width, height] which by default is in normalized coordinates (percentage of figure window). For instance, to add 0.05 units (5% of figure window) to the width, do this:

 p(3) = p(3) + 0.05;
 set(h, 'pos', p);

The SUBPLOT command picks standard values for these parameters, but they could be anything you want. You could put axes anywhere on the figure you want, any size you want.

You can check for it: http://www.mathworks.com/matlabcentral/newsreader/view_thread/144116

Preoccupation answered 8/2, 2015 at 19:13 Comment(2)
This requires playing around with the parameters until you get it right. I would argue that my answer gets it working immediately.Warrantor
Also I'm down voting. Please take no offense. You did a direct copy and paste from that link and showed no effort.Warrantor

© 2022 - 2024 — McMap. All rights reserved.