尝试在 matplotlib 中绘制蜘蛛网图的错误结果
Wrong result trying to plot a spider-web graph in matplotlib
我正在练习如何使用 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 显示它们在边缘前面。
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):
# calculate and plot the edges
edges = np.array([(points[i], points[j]) for i in range(len(points)) for j in range(i)]).reshape(-1, 2)
plt.plot(edges[:, 0], edges[:, 1], color='dodgerblue')
points = np.array(points)
# plot the vertices
plt.scatter(points[:, 0], points[:, 1], color='mediumvioletred', s=100, zorder=3)
plt.axis('equal') # show squares as squares (x and y with the same distances)
plt.axis('off') # hide the surrounding rectangle and ticks
plt.show()
complete_graph(square_points) # square shape
complete_graph(shape_points) # spider web ish shape
我正在练习如何使用 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 显示它们在边缘前面。
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):
# calculate and plot the edges
edges = np.array([(points[i], points[j]) for i in range(len(points)) for j in range(i)]).reshape(-1, 2)
plt.plot(edges[:, 0], edges[:, 1], color='dodgerblue')
points = np.array(points)
# plot the vertices
plt.scatter(points[:, 0], points[:, 1], color='mediumvioletred', s=100, zorder=3)
plt.axis('equal') # show squares as squares (x and y with the same distances)
plt.axis('off') # hide the surrounding rectangle and ticks
plt.show()
complete_graph(square_points) # square shape
complete_graph(shape_points) # spider web ish shape