![Square.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271267/Square.png)
How do I stop patch from making my squares into rectangles?
3 views (last 30 days)
Show older comments
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:
![rectangles.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271262/rectangles.png)
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.
0 Comments
Accepted Answer
Giuseppe Inghilterra
on 12 Feb 2020
Hi,
you can add to your code the following line:
axis equal
In this way you obtain the following result:
![Square.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271267/Square.png)
You can see:
to have control on axes appearance.
Hope this helps.
4 Comments
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.
More Answers (0)
See Also
Categories
Find more on Polygons 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!