AttributeError: 'tuple' object has no attribute 'ndim' matplotlib

AttributeError: 'tuple' object has no attribute 'ndim' matplotlib

我正在尝试使用 python、matplotlib 生成二重积分图。 X 和 Y 范围已定义,Z 是 X 和 Y 的二重积分。

import matplotlib.pyplot as plt
import numpy as np
import scipy
from scipy.integrate import dblquad

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

def function_I(x, y):
    return np.sin(x)*np.cos(y/5)

#Make data
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
Z = scipy.integrate.dblquad(function_I, -5, 5, -5, 5)

# plot the surface
ax.plot_surface(X, Y, Z)

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

ax.plot_surface(X, Y, Z)

给予

AttributeError: 'tuple' object has no attribute 'ndim'

一些类似的问题提供了与 .shape 和 .reshape 相关的解决方案?没有多大意义。任何评论表示赞赏!

dblquad(function_I, -5, 5, -5, 5)计算一个完整面积的积分,因此得到一个值。

以下方法旨在计算连续坐标之间每个小块的积分。然后使用每个面片的平均 XY 位置绘制一个曲面。

import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import dblquad

def function_I(x, y):
    return np.sin(x) * np.cos(y / 5)

# Make data
X = np.linspace(-5, 5, 41)
Y = np.linspace(-5, 5, 41)
X, Y = np.meshgrid(X, Y)
Z = np.zeros_like(X)
for i in range(1, X.shape[0]):
    for j in range(1, X.shape[1]):
        Z[i, j], _ = dblquad(function_I, X[i - 1, j - 1], X[i, j], Y[i - 1, j - 1], Y[i, j])

# plot the surface
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface((X[:-1, :-1] + X[1:, 1:]) / 2, (Y[:-1, :-1] + Y[1:, 1:]) / 2, Z[1:, 1:])

plt.show()