我想在matplotlib画布上交互式绘制多边形。
总体思路:
while clicking left_mouse_button:
draw point on each click and draw a line between 2 latest points
if clicked right_mouse_button:
"close polygon"(connect starting point with the newset point)
到目前为止,这是我的代码:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
x = []
y = []
def onclick(event):
if event.button == 1:
x.append(event.xdata)
y.append(event.ydata)
plt.plot(event.xdata, event.ydata, ',')
plt.scatter(event.xdata, event.ydata)
print(x)
print(y)
for i in range(0, len(x)):
plt.plot(x[i:i+2], y[i:i+2])
if event.button == 3:
plt.plot([x[0], x[-1]], [y[0], y[-1]])
print("")
fig.canvas.draw()
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
我想有一个包含所有点和线的Polygon类,我可以为此使用图形。但是,如何处理分别绘制每个多边形的图呢?我应该如何构造这些类以使其达到最佳状态? 我在想:
class Graph:
points = [Point1, Point2]
class Point:
xcord = 0
ycord = 0
connected_points = [Point1, Point2]
这看起来不错,但我仍然不知道如何处理所有多边形的绘制