Not sure how to use figure('Position',[ ]) to get figures with proper dimensions

I want to make the inner region of the .png have the latitude and longitude axes of the plot roughly equal in size and more square.
This is what I was using to set the position:
figure('Position',[1 1 1100 1100])
And the size of the figure is wider than it is taller, but my goal is to have the inner region of the plot (latitude and longitude axes) to be roughly a square. I was using 'axis square' prior, but some of the plots (that have x, y, or both axes labeled) seem to have different sizes when I use saveas with a .png extension.
I have tried using InnerPosition and OuterPosition in the figure command, but have not had any luck.
Also attached is the figure itself.

2 commentaires

Does ot matter, that the axis labels are not readable? How many pixels does your monitor have? Does this work:
figure('Units', 'pixels', 'Position', [100, 100, 400, 400])
It is not clear, what exacty should be square: The inner position of the figure, which is exported to the PNG file or the axes itself? Literally "seem to have different sizes" mean, that they do have the same size and it only looks differently.
KCE
KCE le 19 Août 2021
Modifié(e) : KCE le 19 Août 2021
I am trying to get the axes itself AND the png file to be the same size (a square). Since there is some white space when it is saved, I want the axis labels to fit between the axes of the plot and the edge of the .png file, so nothing is cut off.
I tried 'axis square' before, but when they were saved, the .png file sizes were different depending on whether or not I had axis labels or not.
Using the above line helped slightly, and now the figure is taller than it is wider. I am using a Mac, so I imagine the pixels should be perfect squares?

Connectez-vous pour commenter.

 Réponse acceptée

DGM
DGM le 19 Août 2021
Modifié(e) : DGM le 19 Août 2021
The margins needed for the labels/title etc may vary, so the inner position will vary. If you can come up with an inner position value which allows for the worst-case (e.g. large tick labels), then you can specify it for all plots.
x = 0:9;
plot(x,x);
% set figure size/location in pixels; doesn't work if figure is docked
set(gcf,'units','pixels','outerposition',[1 1 500 500],'windowstyle','normal')
% set inner position (the plot box); force it square
set(gca,'units','normalized','position',[0.1300 0.1100 0.7750 0.8150], ...
'plotboxaspectratio',[1 1 1])
Of course, plots with short labels or no labels will have excess padding, but that's necessarily the result of making the geometry fixed.
This is a composite of two different plots, showing that the saved images are the same size and the plot boxes are in the same locations
Depending on your version, there may be properties like 'positionconstraint' which may be relevant. I'm running an older version, so you may have to extrapolate.

8 commentaires

This is MATLAB 2017b, so I tried using pbaspect([1 1 1]) instead, but that did not seem to actually adjust anything with the axes and force it to be a square, even after I had the figure displayed on my screen.
At this point, I just want to make the plot box itself square (length of x and y axes to be the same), and keep that consistent regardless of what axis labels are present.
I made a minor change to the property names. Sometime around then, the 'innerposition' property changed, but it should've thrown an error if that's what the issue was. Using 'position' should work in new and old versions. I tested the above example in R2009b, R2015b, and R2019b.
Thanks for the code, that seems to be helping! What exactly is figure 'normalizing' to? would that be consistent with plots that whether or not they have axis labels?
In the example, the figure isn't using normalized coordinates. It's in pixels. The axes is using normalized coordinates, which are relative to the figure. Since the axes position is being fixed, it shouldn't change WRT the figure, regardless of the presence or size of tick labels/titles/etc. You'll obviously need to accomodate for those things when picking the axes position, otherwise they'll get cut off. Just to make sure nothing gets adjusted after the fact, you'd want to place title, axis labels, etc before adjusting the axes/figure position.
I still seem to be getting different margin sizes even with those two lines for some reason...
% set figure size/location in pixels; doesn't work if figure is docked
set(gcf,'units','pixels','outerposition',[1 1 1000 1000],'windowstyle','normal')
% set inner position (plot box); force it square
set(gca,'units','normalized','position',[0.1300 0.1100 0.7750 0.8150],'plotboxaspectratio',[1 1 1])
I have a line above that initially sets the size;
figure('Units','pixels','Position',[100 100 975 975]);
Should I get rid of this, or just put "figure;"
Well, they're setting the same property, so you should be able to use one or the other (just pick which position vector you want). In what cases are the margins changing?
I have a few different .m scripts where I want to have figures come out as the same size. I changed the first line to just 'figure;' and that did not seem to solve the issues. Both scripts have identifcal lines to what I posted above. The text labels are all the same font size too.
Can you come up with a simplified example to demonstrate how it's failing? I can see if I can replicate it and maybe try to fix it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Properties dans Centre d'aide et File Exchange

Question posée :

KCE
le 19 Août 2021

Commenté :

DGM
le 24 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by