python绘制隐式函数f(x,y)=0,其中x,y进行矩阵乘法

2024-10-02 04:24:06 发布

您现在位置:Python中文网/ 问答频道 /正文

作为隐式函数,其中“A”是n*2矩阵

0 = np.dot((x,y),A)

0=xA11yA12

0=xA21yA22

0=xAn1yAn2

是否可以通过matplotlib或其他方式在同一绘图上绘制所有线,而不使用大循环


Tags: 函数绘图matplotlibnp方式绘制矩阵dot
1条回答
网友
1楼 · 发布于 2024-10-02 04:24:06

给定一个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()

example plot

相关问题 更多 >

    热门问题