python - 如何在给定长度的曲线的每个点上绘制法线向量?
问题描述
如何在给定长度的曲线的每个点上绘制法线向量?
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [8, 8]
x = np.linspace(-1, 1, 100)
y = x**2
ax.set_ylim(-0.3, 1.06)
ax.plot(x, y)
plt.show()
解决方案
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [8, 8]
x = np.linspace(-1, 1, 100)
y = x**2
# Calculating the gradient
L=.1 # gradient length
grad = np.ones(shape = (2, x.shape[0]))
grad[0, :] = -2*x
grad /= np.linalg.norm(grad, axis=0) # normalizing to unit vector
nx = np.vstack((x - L/2 * grad[0], x + L/2 * grad[0]))
ny = np.vstack((y - L/2 * grad[1], y + L/2 * grad[1]))
# ax.set_ylim(-0.3, 1.06)
ax.plot(x, y)
ax.plot(nx, ny, 'r')
ax.axis('equal')
plt.show()
推荐阅读
- r - 在 R 中“分隔”一列的更简洁的选项(可能通过一些正则表达式)?
- asp.net - asp.net core 版本 5 样板正在生产中
- python - n维数组元素的Python组合
- logging - 如何在 Jupyterlab 中登录到日志控制台?
- php - 使用 Bash 并调用 php 命令遍历目录和子目录中的所有文件
- firebase - 当我以这种方式通过本机反应将日期存储在firebase中时,它不会保存,只有购物车项目和总金额仅保存在firebase中
- javascript - 如何在没有项目/坐标重复的情况下选择 2D 矩阵的 N 个随机元素的 col/row 或 x/y 坐标?
- sql-server - DDL 触发器防止丢包
- python - pypi没有安装
- bash - 将多个变量添加到关联数组