# How do I stop patch from making my squares into rectangles?

2 views (last 30 days)
John Smith on 12 Feb 2020
Commented: Walter Roberson on 13 Feb 2020
Hi all,
I am trying to call patch multiple times to draw multiple squares. I am using patch because even though I am currently drawing squares, I am planning to draw a more complex polygon later on.
However I am facing a problem: even though the coordinates should create a 1x1 size square, matlab is stretching my polygon and drawing a rectangle. Additionally, I want my squares to be bigger, but changing my 1x1 square to a 2x2 square does not change the size. How can I adjust the polygon drawn by patch? It's ok for my squares to resize if I maximize/ stretch the figure, but I want some control over the initial size and shape.
Here is some code which produces the unexpected result:
x1 = [0 1 1 0];
y1 = [0 0 1 1];
x2 = [1.2 2.2 2.2 1.2];
y2 = [0 0 1 1];
x3 = [2.4 3.4 3.4 2.4];
y3 = [0 0 1 1];
patch(x1,y1, 'red')
patch(x2,y2, 'blue')
patch(x3,y3, 'green')
Here is the figure: I can tell by reading the axes of the chart that my 'squares' are 1x1, but the figures drawn are rectangles. I want to make them squares.
I would really appreciate help with this. I have tried both 'truesize' and set(gcf,'Resize','off') but they stop the figure from resizing and do not force my squares to be 1 pixel by 1 pixel as I expected.

Giuseppe Inghilterra on 12 Feb 2020
Hi,
axis equal
In this way you obtain the following result: You can see:
to have control on axes appearance.
Hope this helps.

Show 1 older comment
Walter Roberson on 13 Feb 2020
Use xlim() and ylim() to control which part of the axes is viewed. If you look at a smaller part of the axes, then the objects contained in that area would look larger.
Set the axes Position property to control where (and how big) in the figure it shows up. If you give more figure space to an axes, then the objects in it will show up larger.
Set the figure Position property to control how much screen space is allocated to the figure. If you give more screen space to a figure, then the objects in it will show up larger.
Note: if you are using subplot() then you do not have much control over the Position of the axes without risking deleting existing graphics.
John Smith on 13 Feb 2020
Thank you! I am using subplot, that may be why this is so difficult.
I was able to set the axes Positon to make my patches bigger within the figure. For anyone else who has this problem, this is the syntax:
set(gca,'units','pix'); % Set axes units to pixels
set(gca,'position', [20 50 300 300]); % Set axes positions
You then have to manually set the position to make the plot big enough. It's not ideal but it works.
Walter Roberson on 13 Feb 2020
subplot() has the challenge that each time you call subplot(), it calculates where the given axes should be according to division up into the numbers you gave, and then it searches to see if there are any existing axes that cover that location. If there is an existing axes with the exact same location, then the existing axes is used. If there is an existing axes that overlaps any of the existing area but is not in the exact same location, then the existing axes is deleted.
The practical implications of this are that if you use subplot, you need to call it for each axes location and record the axes handles, and only after you have generated and recorded them all, you can change the axes Position properties. The checking for overlap is not done each time Position is changed, only when subplot() is called, so you have to be sure not to call subplot() that would overlap the area after you change Position.