当我更改坐标时,为什么 Shapely 会绘制两条线?

Fal*_*alc 6 python matplotlib shapely

我正在尝试了解 Shapely 的工作原理。

我可以用下面的代码画一条简单的线:

import matplotlib.pyplot as plt

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

plt.plot(AB)
Run Code Online (Sandbox Code Playgroud)

但是当我改变坐标时:

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

plt.plot(AB)
Run Code Online (Sandbox Code Playgroud)

Shapely 决定绘制两条线,这是我不理解的行为。

使用Shapely 1.7.0

截屏

小智 8

您使用plt.plot()方法不正确。

所做plt.plot()的是将 y 与 x 绘制为线条和/或标记。

在文档中,您可以看到,由于调用plot(AB)只有 1 个参数,AB因此作为 Y 值传递。在本例中,X 值是 Y 值数组中元素的索引。

这与调用相同plt.plot([(1,0),(3,4)])。由于您有 2 个 Y 值元组,因此您将得到 2 条不同的行:[(0,1),(1,3)][(0,0),(1,4)]。(注意 x 值是 0 和 1,即 Y 值对应元组的索引。)

您可以在输出的屏幕截图中看到,在第一种情况下,您还绘制了 2 条线。但在这些特定值的情况下,plt.plot([(0,0),(1,1)])将绘制同一条线两次。

如果您只想绘制从 A 点到 B 点的直线,可以使用:

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

plt.plot(*AB.xy)

plt.show()
Run Code Online (Sandbox Code Playgroud)