'Why is Shapely plotting two lines when I change a coordinate?
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
Solution 1:[1]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user3100212 |

