python matplotlib quiver

matplotlib中的 quiver方法可用于绘制箭头(向量),下面介绍二维和三维中的使用方法

二维箭头向量绘制

一般参数如下

quiver(X, Y, U, V, **kw)

参数的含义如下图所示,其中XY决定箭头尾部的位置,UV决定箭头的方向,可以理解为以箭头尾部为起点,沿X和Y轴的分量。
在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 40)
y = x ** 2 * np.exp(-x)
u = np.array([x[i + 1] - x[i] for i in range(len(x) - 1)])
v = np.array([y[i + 1] - y[i] for i in range(len(x) - 1)])
x = x[:len(u)]  # 使得维数和u,v一致
y = y[:len(v)]
c = np.random.randn(len(u))  # arrow颜色
plt.figure()
plt.quiver(x, y, u, v, c, angles='xy', scale_units='xy', scale=1)
plt.show()

在这里插入图片描述

三维箭头向量绘制

三维和二维类似,只不过多了一个维度,参数的意义是一样的

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制点
num_points = 20
pts = np.random.rand(num_points, 3)
ax.scatter(pts[:, 0], pts[:, 1], pts[:, 2], c='b', s=3, linewidth=0, alpha=1, marker="o")

# 绘制箭头
direction = np.random.rand(num_points, 3)
ax.quiver(pts[:, 0], pts[:, 1], pts[:, 2],
          direction[:, 0], direction[:, 1], direction[:, 2],
          length=0.05, normalize=True, color='r')
plt.show()

在这里插入图片描述

参考
  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值