我正在绘制2D功能,使用:
import numpy as np
import matplotlib.pyplot as plt
# Build x,y mesh
nx = 1000; ny = 1000
x = np.linspace(-0.5, 0.5, nx)
y = np.linspace(-0.5, 0.5, ny)
X,Y = np.meshgrid(x, y)
# Evaluate the Gaussian on grid
func = np.exp(-np.pi*((X)**2 + (Y)**2))
plt.imshow(func); plt.colorbar()
此时,我正在将函数集成到meshgrid的x
和y
向量的整个范围内,包括正确的dx
和dy
增量。该值显示在标题中:
# Integrate the function
intval = np.trapz(np.trapz(func, y, dx=x[1]-x[0]), x, dx=x[1]-x[0])
plt.title('Integrated value = %s' % intval)
如何在窗口的部分上集成相同的功能?比方说,在x [400:600]和y [400:600]之间?
答案 0 :(得分:1)
如何传递数组的那部分?
cut = slice(400, 600)
intval = np.trapz(np.trapz(func[cut, cut], y[cut], dx=x[1]-x[0]),
x[cut], dx=x[1]-x[0])