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

6 views (last 30 days)
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
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.

Accepted Answer

Giuseppe Inghilterra
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
You can see:
to have control on axes appearance.
Hope this helps.
  4 Comments
John Smith
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
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!