tight window with axis('equal')

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
Yair Altman on 3 Mar 2016
Check whether the axes' undocumented LooseInset property can help you: http://undocumentedmatlab.com/blog/axes-looseinset-property

1 Comment

Doesn't seem to help in this case, but thanks for the suggestion.

Sign in to comment.

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

Thanks for help John. But I was using "get" to get the TightInset not "set", so my code works. Also your code does not include the axis('xy','equal'). If this is added to your code, it does the same thing my code does.
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?
I'm don't entirely understand your grammar. But I do not want any whitespace to the left or right of the image. I also need to use the axis('xy','equal') command, which seems to be causing the trouble.
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
As you experienced, there always seems to be whitespace added even if you try to use a different method to save it. As for the imwrite idea, I actually oversimplified my example code. I am using the imagesc command as well as scatter to plot some points. So these points would not show up using imwrite.
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
Sorry I wasn't completely accurate in what I am trying to do. I am overlaying a scatter plot on top of the imagesc plot. The code is more like this
figure;
xv = 0:.1:10;
yv = 0:.1:10;
F = rand(101,101);
imagesc(xv, yv, F);
hold on;
set(gca,'XTickLabel',[]);
set(gca,'YTickLabel',[]);
axis('equal','xy');
scatter([2 5 7],[1 6 3],[80 80 80],'r','filled');
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)]);
In this case, I don't believe imwrite can save everything in the resulting plot window.
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')
Thanks, I could use this as a work around. Do you know if it is possible to include ticks and other axis things using this technique?

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

on 15 Feb 2016

Commented:

on 15 Apr 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!