我正在练习如何使用matplotlib和pyplot库,正是出于这个原因,我正在尝试制作一个函数来绘制点,使任意两点都有一条 连接它们
我想我快要解决这个问题了,但结果似乎还是有点不对劲
我的代码是:
import numpy as np
import matplotlib.pyplot as plt
alpha = (np.sqrt(2)/2)
square_points = ((0, 0),(1, 0),(0, 1),(1, 1))
shape_points = ((1, 0),(alpha, alpha),(0, 1),(-alpha, alpha),(-1, 0),(-alpha, -alpha),(0, -1),(alpha, -alpha))
def complete_graph(points):
for i in range(len(points)):
for j in range(i):
x = (points[i])
y = (points[j])
plt.plot(x, y)
plt.show()
complete_graph(square_points) #square shape
complete_graph(shape_points) #spider web ish shape
结果应该是这样的: 方形
蜘蛛网形状
然而,我的结果是:
你需要分别得到x和y坐标。最简单的是
x=[points[i][0], points[j][0]]
和y=[points[i][1], points[j][1]]
使用numpy,可以编写创建所有x的代码。可以使用
plt.scatter()
绘制顶点。将z顺序设置为3会在边的前面显示它们相关问题 更多 >
编程相关推荐