-1

I try to plot and connect points in in the shape of a rectangle, but I am doing something wrong.

I have vectors of coordinates like this:

x = [6.2372045620000005, 6.237194762000001, 6.237194762000001, 6.2372045620000005]

y = [51.071833453, 51.071835828999994, 51.071833453, 51.071835828999994]

First, I plot point data:

plt.scatter(x, y, color = 'blue')

enter image description here

Then, I try to add line between points in such a way, that a rectangle is formed. Unfortunately this below does not work correctly.

plt.scatter(x, y, color = 'blue')
plot.plot(x,y)

enter image description here

Do you know what I am doing wrong? It's a simple thing for sure, but I'm stuck with that..

Thanks for you help and comments.

sdom
  • 319
  • 1
  • 9
  • **[Don't Post Screenshots](https://meta.stackoverflow.com/questions/303812/)**. Always provide a [mre], with **code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. It's likely the question will be down-voted and closed. You're discouraging assistance, as no one wants to retype data/code, and screenshots are often illegible. [edit] the question and **add text**. Plots are ok. See [How to provide a reproducible copy of your DataFrame using `df.head(30).to_clipboard(sep=',')`](https://stackoverflow.com/questions/52413246). – Trenton McKinney Sep 08 '21 at 12:59
  • What have you tried? SO is not a coding service. You're expected to [try to solve the problem first](https://meta.stackoverflow.com/questions/261592) and show your effort. – Trenton McKinney Sep 08 '21 at 13:03
  • What do you mean with "on opposite values"? I think you can easily find the coordinates of other corners, right? Then just plot a line which pass on all corners. – Giacomo Catenazzi Sep 08 '21 at 14:30
  • Hi @GiacomoCatenazzi I just edited my post, would you please take a look again? – sdom Sep 09 '21 at 13:18

1 Answers1

1

Usually it is assumed that the coordinates, for any shape, are already ordered.

If you're confident that you have a rectangle (all right corners, not some quadrilateral) you can take the min/max values of your x and y coordinates to get the correct corner points. Given the coordinates that you already defined in your post:

import matplotlib.patches as patches
import matplotlib.pyplot as plt

xmin = min(x)
xmax = max(x)
xsize = xmax - xmin

ymin = min(y)
ymax = max(y)
ysize = ymax - ymin

Using Matplotlib, you can either use the Rectangle patch like:

fig, ax = plt.subplots(dpi=86)
ax.scatter(x, y, color='blue')

rect = patches.Rectangle(
    (xmin, ymin), xsize, ysize, 
    lw=2, edgecolor='r', facecolor='none',
)
ax.add_patch(rect)

Or the Polygon patch, for any arbitrary shape. Both methods will work in this case:

points = [
    (xmin, ymin),
    (xmax, ymin),
    (xmax, ymax),
    (xmin, ymax),
]

poly = patches.Polygon(
    points, lw=2, edgecolor='r', facecolor='#ff000011', closed=True,
)
ax.add_patch(poly)

enter image description here

If you do have something less regular shaped, like a quadrilateral, calculating the convex-hull might help getting the correct order of the coordinates. See for example:
Convex hull of 4 points

Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97