给定一个n*2 矩阵A,对于每一行i,一行由A[i,0]*x + A[i,1]*y == 0 定义。这意味着0,0 总是在线上,点x=A[i,1],y=-A[i,0] 也一样。乘以任何值,例如通过规范化将再次给出线上的点。
以下代码显示了 3 种可视化这些线条的方法:
- 一些被圆切割的线段,以及
x=A[i,1],y=-A[i,0]和x=-A[i,1],y=A[i,0]。
- 相同的线段一直延伸到绘图的边界。
- 只是圆上的一些端点。
import matplotlib.pyplot as plt
import numpy as np
from numpy.linalg import norm
from matplotlib.collections import LineCollection
n = 10
radius = 20
A = np.random.uniform(-10, 10, (n, 2))
B = A / norm(A, axis=1, keepdims=True) * radius # normalize and put on a circle with given radius
lines = np.dstack([B[:, 1], -B[:, 0], -B[:, 1], B[:, 0]]).reshape(-1, 2, 2)
fig, axes = plt.subplots(ncols=3, figsize=(14, 4))
for ax in axes:
ax.set_aspect('equal')
for ax in axes[:2]:
lc = LineCollection(lines, colors='blue', linewidths=2)
ax.add_collection(lc)
if ax == axes[0]:
ax.scatter(A[:, 1], -A[:, 0], color='crimson')
ax.scatter(-A[:, 1], A[:, 0], color='crimson')
elif ax == axes[1]:
ax.set_xlim(-radius / 2, radius / 2)
ax.set_ylim(-radius / 2, radius / 2)
for k in range(2):
axes[2].scatter(lines[:, k, 0], lines[:, k, 1], color='crimson')
axes[0].set_title('lines in circle and dots')
axes[1].set_title('lines till border')
axes[2].set_title('dots on circle')
plt.show()