gpt4 book ai didi

python - 在python中绘制方向字段

转载 作者:行者123 更新时间:2023-12-04 16:08:54 25 4
gpt4 key购买 nike

我想为一个简单的方程绘制方向场:

y'  = 3 − 2y

我发现了类似的 Matlab 问题 here (1.3)。但我不知道如何将其重写为 python。我的最后一次尝试是:
from matplotlib.pyplot import cm
import matplotlib.pyplot as plt
import numpy as np

nx, ny = .3, .3
x = np.arange(-3, 3, nx)
y = np.arange(-2, 2, ny)
X, Y = np.meshgrid(x, y)

dy = X + np.sin(Y)
dx = np.ones((10,10))

plot2 = plt.figure()
plt.quiver(X, Y, dx, dy,
color='Teal',
headlength=7)

plt.title('Quiver Plot, Single Colour')
plt.show(plot2)

但我收到错误:
builtins.ValueError: operands could not be broadcast together with shapes (100,) (280,) 

我虽然它会很简单,但经过几个小时的搜索如何绘制一个简单的方向字段后,我很沮丧。

最佳答案

dxdy必须与 X 的形状相同和 Y .

目前,您的形状为 (14, 20)X , Ydy ,但是 (10,10)dx .

如果您更改定义 dx 的行到:

dx = np.ones(dy.shape)

一切正常:

enter image description here

关于python - 在python中绘制方向字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39619128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com