tight window with axis('equal')
Show older comments
I have a plot which needs to have equal axes. I want to make the figure window tight around this plot so I can put it in a document. The code below attempts to do this, but fails because it leaves a margin on the left and right. Does anyone know of a way to do this?
figure;
xv = 0:.1:10;
yv = 0:.1:10;
F = rand(101,101);
imagesc(xv, yv, F);
set(gca,'XTickLabel',[]);
set(gca,'YTickLabel',[]);
axis('equal','xy');
axis([0 10 0 10]);
ti = get(gca,'TightInset');
set(gca,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);
Answers (2)
Yair Altman
on 3 Mar 2016
1 vote
Check whether the axes' undocumented LooseInset property can help you: http://undocumentedmatlab.com/blog/axes-looseinset-property
John BG
on 16 Feb 2016
i grab the axis handle with
ax=gca
set(gca,'TightInset',[0 0 0 0]);
does not work because TightInset is read only.
try
ax.Position=[0 0 1 1]
this nulls above and below, yet there's still a bit of void on left and right that you may want to get rid of. If you open
propertyeditor('on')
PaperSize is [21 29.7] may be you want the paper set paper square shape.
In any case, write the following
xv = 0:.1:10
yv = 0:.1:10
F = rand(101,101)
imagesc(xv, yv, F)
axis([0 10 0 10])
ax=gca
ax.Position
ax.Position=[0 0 1 1]

when you save the image, not as .fig, but as .jpg, it seems quite tight to me.
If you find this answer of any help solving your question, please click on the above thumbs-up vote link, thanks in advance
John
9 Comments
Trevor
on 16 Feb 2016
John BG
on 18 Feb 2016
please comment, problem you mention in the question is the margin left on either side, because you want to insert this image in a document, and you not want to waste document space, right?
Trevor
on 18 Feb 2016
John BG
on 19 Feb 2016
Then avoid using the figure editor or imagesc, let me explain:
1.- remove all void space around the image with:
ax.PlotBoxAspectRatioMode='manual'
ax.PlotBoxAspectRatio=[1 1 1]
ax.DataAspectRatioMode='manual'
ax.DataAspectRatio=[1 1 1]
ax.Position=[0 0 1 1]
This is instead of axis('xy','equal'). Now both data and axes have aspect ratio is 1:1 .
There are no void margins on either side of the image, while in the figure editor.
2.- Yet check the following:
ax.OuterPosition
ans = -0.17 -0.13 1.29 1.23
The figure editor adds left and right void space. You can remove these left and right margins with
ax.OuterPosition=[0 0 1 1]
3.- However, as you attempt to save it from the figure editor, the figure editor will add space for the axis labels and for possible titles and legend boxes, even if there are none, it's the way the figure editor works.
4.- Then you may consider printing instead. After trying different values I reached the following parameters for a tight frame, with custom paper size:

Yet every time I printed to PDF, the PDF driver saving it to image format so that you can later on insert in a document adds void space, ignoring the tight frame defined.
If you have a print-to-file driver that allows you more options than the one I have, you may manage to print to paper with exactly the size of the image, as attempted with the figure editor preview set up, yet it did not work as expected.
5. Alternatively, you can generate the image and save it directly. You generated the colour map with imagesc. I prefer true color RGB to save directly to .jpg:
F21=randi([0 255],101,101)
F22=randi([0 255],101,101)
F23=randi([0 255],101,101)
F21=uint8(F21)
F22=uint8(F22)
F23=uint8(F23)
F2(:,:,1)=F21
F2(:,:,1)=F22
F2(:,:,1)=F23
imwrite(F2,'image_random_colour25.jpg')
Now the saved image has absolutely no void margins at all as you asked in the question.
If you find this answer of any help solving your question, please click on the thumbs-up vote, thanks in advance
John
Trevor
on 19 Feb 2016
John BG
on 20 Feb 2016
Trevor,
you said you want to save the image without the void space on each sides. imwrite does precisely what you asked for: save the image without anything around it.
I have showed you a simple way to .jpg save an image randomly generated in a similar way you do in your question.
Now you mention code you skipped in the initial question, whatever you do with imagesc and scatter, whatever are the points you modify, if they are within the square you defined as image, then they will show up in the saved image with imwrite, don't you agree?
If whatever processing you perform is outside the image, could you be more specific and detail the code that, the image processing out of it, you think would not be included in imwrite?
And since I showed you a way to save the image you generate in the question removing the void spaces you didn't know how to remove? do you agree I deserve your click vote on the thumbs-up?
regards
John
Trevor
on 22 Feb 2016
Mike Garrity
on 22 Feb 2016
You can use getframe to get the contents of the plot window, and then use imwrite to save that:
imshow('street1.jpg')
hold on
scatter(640*rand(1,150),480*rand(1,150),36,rand(1,150),'filled')
img = getframe(gcf)
imwrite(img.cdata,'myplot.png')

Trevor
on 22 Feb 2016
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!