numpy 太多的值要解包,matplotlib,pyplot.quiver

k7fdbhmy  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(120)

我试图绘制两个并排的箭图,然而,它似乎失败了第一个,错误消息ValueError: too many values to unpack (expected 2)我已经查看了matplotlib文件,它似乎是一个错误,在检查我的箭图U参数的大小。有人能帮帮我吗?下面是我的代码:

import numpy as np
import matplotlib.pyplot as plt

spring_const = 20
mass = 1
initial_length = 1
mu = 3
g = 9.81

size, spacing = 10, 0.5

gamma = theta, theta_dot, length, length_dot = np.meshgrid(np.arange(-size, size, spacing), np.arange(-size, size, spacing), np.arange(-size, size, spacing), np.arange(-size, size, spacing))

theta_2dot = (1/(length+1e-10)) * (g * ((theta*np.cos(theta) - np.sin(theta))) + ((spring_const / mass)*theta*(initial_length - length)) - ((mu / mass)*(length_dot**2)*((theta_dot**2) + 1)) - (length_dot*theta_dot))
length_2dot = ((spring_const / mass)*(length - initial_length)) - (g*np.cos(theta))

nabla_gamma = theta_dot, theta_2dot, length_dot, length_2dot

fig, (ax0, ax1) = plt.subplots(1, 2)

for i in gamma:
    print(i.shape)
print("")
for i in nabla_gamma:
    print(i.shape)

ax0.quiver(theta, theta_dot, theta_dot, theta_2dot)
ax1.quiver(length, length_dot, length_dot, length_2dot)

plt.show()

下面是Python输出的内容,包括错误:

(40, 40, 40, 40)
(40, 40, 40, 40)
(40, 40, 40, 40)
(40, 40, 40, 40)

(40, 40, 40, 40)
(40, 40, 40, 40)
(40, 40, 40, 40)
(40, 40, 40, 40)
Traceback (most recent call last):
  File "d:\FILES\Programming\python\Maths\Vector Fields\main.py", line 27, in <module>
    ax0.quiver(theta, theta_dot, theta_dot, theta_2dot)
  File "matplotlib\__init__.py", line 1459, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "matplotlib\axes\_axes.py", line 5167, in quiver
    q = mquiver.Quiver(self, *args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "matplotlib\quiver.py", line 478, in __init__
    X, Y, U, V, C = _parse_args(*args, caller_name='quiver')
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "matplotlib\quiver.py", line 423, in _parse_args
    nr, nc = (1, U.shape[0]) if U.ndim == 1 else U.shape
    ^^^^^^
ValueError: too many values to unpack (expected 2)

我添加了print(i.shape)部分,试图找出数组大小是否有错误。但它们都是一样的,所以我很困惑。

lb3vh1jj

lb3vh1jj1#

问题在于箭图函数的输入阵列的形状。箭图函数的参数需要2D数组,但在代码中,theta、theta_dot、theta_2dot、length、length_dot和length_2dot都是4D数组。您可以尝试在将这些数组传递给 Flutter 函数之前将其整形为2D

import numpy as np
import matplotlib.pyplot as plt

spring_const = 20
mass = 1
initial_length = 1
mu = 3
g = 9.81

size, spacing = 10, 0.5

gamma = theta, theta_dot, length, length_dot = np.meshgrid(np.arange(-size, size, spacing), np.arange(-size, size, spacing), np.arange(-size, size, spacing), np.arange(-size, size, spacing))

theta_2dot = (1/(length+1e-10)) * (g * ((theta*np.cos(theta) - np.sin(theta))) + ((spring_const / mass)*theta*(initial_length - length)) - ((mu / mass)*(length_dot**2)*((theta_dot**2) + 1)) - (length_dot*theta_dot))
length_2dot = ((spring_const / mass)*(length - initial_length)) - (g*np.cos(theta))

nabla_gamma = theta_dot, theta_2dot, length_dot, length_2dot

fig, (ax0, ax1) = plt.subplots(1, 2)

for i in gamma:
    print(i.shape)
print("")
for i in nabla_gamma:
    print(i.shape)

theta = theta.reshape((theta.shape[0], -1))
theta_dot = theta_dot.reshape((theta_dot.shape[0], -1))
theta_2dot = theta_2dot.reshape((theta_2dot.shape[0], -1))
length = length.reshape((length.shape[0], -1))
length_dot = length_dot.reshape((length_dot.shape[0], -1))
length_2dot = length_2dot.reshape((length_2dot.shape[0], -1))

ax0.quiver(theta, theta_dot, theta_dot, theta_2dot)
ax1.quiver(length, length_dot, length_dot, length_2dot)

plt.show()

相关问题