An alternative that is better in my opinion is to use patch, for example:
imagesc(rand(10,10)), hold on
vert = 0.5+[0 0; 1 0; 1 1; 0 1]; % x and y vertex coordinates
fac = [1 2 3 4]; % vertices to connect to make square
patch('Faces',fac,'Vertices',vert,'FaceColor','none','LineWidth',2)
vert2 = 0.5+[5 6; 5 8; 9 8; 9 5; 7 5; 7 6]; % x and y vertex coordinates
fac2 = [1 2 3 4 5 6 ]; % vertices to connect to make the other closed polygon
patch('Faces',fac2,'Vertices',vert2,'FaceColor','none','LineWidth',2)

Note that the reason I added 0.5 to the coordinates of the vertices is because the imagescbins are centered around integer values, so the edges of the bin are 0.5 values.
source
share