【问题标题】:python - Plotting implicit function f(x,y) = 0, where x,y undergo matrix multiplicationpython - 绘制隐式函数 f(x,y) = 0,其中 x,y 进行矩阵乘法
【发布时间】:2020-08-13 16:20:10
【问题描述】:

作为隐函数,其中 'A' 是一个 n*2 矩阵

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

0 = xA11 yA12

0 = xA21 yA22

...

0 = xAn1 yAn2

是否有可能通过 matplotlib 或其他方式在没有大循环的情况下在同一个图上绘制所有线?

【问题讨论】:

  • 0 = np.dot((x,y),A) 不是有效的 Python 代码。你能澄清你的问题吗?

标签: python function matplotlib plot implicit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多