![](/img/trans.png)
[英]How to plot a 3D surface Plot using three 1D arrays in python as I am getting an error Z must be in 2 dimensional. How to convert it into 2D array?
[英]How can I interpolate missing values (undefined areas) of a 3D surface plot (2D array) using Python?
在使用Numpy和matplotlib的Python 3.7中,我想为以下方程式绘制3D曲面:
显然,该函数在x = 0或y = 0时未定义。
要计算和绘制此图,我有以下代码,当前在Jupyter Notebook中运行:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
%matplotlib notebook
f = lambda x, y: np.sin(x)*np.sin(y)/(x*y)
xs, ys = np.mgrid[-np.pi:np.pi:31j, -np.pi:np.pi:31j]
zs = f(xs, ys)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X=xs, Y=ys, Z=zs)
如何对缺失值进行插值,以使图形显得平滑?
Scipy具有可以执行此操作的插值模块。 依靠以上内容(在问题发布中),此代码可以在下一个单元格中运行:
from scipy import interpolate
# integer arrays for indexing
x_indx, y_indx = np.meshgrid(np.arange(0, zs.shape[1]),
np.arange(0, zs.shape[0]))
# mask all invalid values
zs_masked = np.ma.masked_invalid(zs)
# retrieve the valid, non-Nan, defined values
valid_xs = x_indx[~zs_masked.mask]
valid_ys = y_indx[~zs_masked.mask]
valid_zs = zs_masked[~zs_masked.mask]
# generate interpolated array of z-values
zs_interp = interpolate.griddata((valid_xs, valid_ys), valid_zs.ravel(),
(x_indx, y_indx), method='cubic')
# finally, plot the data
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X=xs, Y=ys, Z=zs_interp)
请注意,此代码已针对可读性和可理解性(而非内存效率)进行了优化。 重新优化此代码以提高内存效率是一项琐碎的任务,留给读者
在这种情况下,您可以使用scipy.special.sinc
。 这将插入精确结果sin(0)/0 = 1
:
import numpy as np
from scipy.special import sinc
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
%matplotlib notebook
f = lambda x, y: sinc(x)*sinc(y)
xs, ys = np.mgrid[-1:1:31j, -1:1:31j]
zs = f(xs, ys)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X=xs*np.pi, Y=ys*np.pi, Z=zs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.