5

I'm trying to understand how Shapely works.

I can draw a simple line with the following code:

import matplotlib.pyplot as plt

A = Point(0,0)
B = Point(1,1)
AB = LineString([A,B])

plt.plot(AB)

However when I alter the coordinates:

A = Point(1,0)
B = Point(3,4)
AB = LineString([A,B])

plt.plot(AB)

Shapely decides to plot two lines, which is behaviour I don't understand.

Using Shapely 1.7.0

Screenshot

Georgy
  • 12,464
  • 7
  • 65
  • 73
Falc
  • 307
  • 2
  • 14
  • Related: [How do I plot Shapely polygons and objects using Matplotlib?](https://stackoverflow.com/q/55522395/7851470) – Georgy Feb 10 '20 at 11:11

1 Answers1

7

You are using plt.plot() incorrectly.

What plt.plot() does is Plot y versus x as lines and/or markers.

In the docs, you can see that since the call plot(AB) has only 1 argument, AB is being passed as the Y values. The X value, in this case, is the index of the elements in the array of Y values.

It is the same as calling plt.plot([(1,0),(3,4)]). Since you have 2 tuples of Y values, you will get 2 different lines: [(0,1),(1,3)] and [(0,0),(1,4)]. (Notice the x values are 0 and 1, the index of the corresponding tuple of Y value.)

You can see in the screenshot of the output, that in the first case you also plot 2 lines. But in the case of these specific values, plt.plot([(0,0),(1,1)]) will plot the same line twice.

If you just want to graph a line from point A to point B, you can use:

A = Point(1,0)
B = Point(3,4)
AB = LineString([A,B])

plt.plot(*AB.xy)

plt.show()
user3100212
  • 101
  • 1
  • 5
Moshe perez
  • 1,626
  • 1
  • 8
  • 17