旋转 3d 曲面图时散点消失
Scatter points are disappearing when rotating a 3d surface plot
我试图通过绘制所有内容并旋转表面来检查表面行为相对于 3d 中的散点的任何奇怪之处,从而了解表面与我的数据点的拟合程度 space.
问题是当我旋转渲染来执行此操作时,绘图消失了。我怎样才能让剧情持续下去?
您可以使用以下代码进行复现 - 主要取自 Python 3D polynomial surface fit, order dependent.
的惊人答案
import numpy as np
import scipy.linalg
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import itertools
def main():
# Generate Data...
numdata = 100
x = np.random.random(numdata)
y = np.random.random(numdata)
z = x**2 + y**2 + 3*x**3 + y + np.random.random(numdata)
# Fit a 3rd order, 2d polynomial
m = polyfit2d(x,y,z)
# Evaluate it on a grid...
nx, ny = 20, 20
xx, yy = np.meshgrid(np.linspace(x.min(), x.max(), nx),
np.linspace(y.min(), y.max(), ny))
zz = polyval2d(xx, yy, m)
# Plot
#plt.imshow(zz, extent=(x.min(), y.max(), x.max(), y.min()))
#plt.scatter(x, y, c=z)
#plt.show()
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, y, z, color='red', zorder=0)
ax.plot_surface(xx, yy, zz, zorder=10)
ax.set_xlabel('X data')
ax.set_ylabel('Y data')
ax.set_zlabel('Z data')
plt.show()
text = "filler"
def polyfit2d(x, y, z, order=4):
ncols = (order + 1)**2
G = np.zeros((x.size, ncols))
#ij = itertools.product(range(order+1), range(order+1))
ij = xy_powers(order)
for k, (i,j) in enumerate(ij):
G[:,k] = x**i * y**j
m, _, _, _ = np.linalg.lstsq(G, z)
return m
def polyval2d(x, y, m):
order = int(np.sqrt(len(m))) - 1
#ij = itertools.product(range(order+1), range(order+1))
ij = xy_powers(order)
z = np.zeros_like(x)
for a, (i,j) in zip(m, ij):
z += a * x**i * y**j
return z
def xy_powers(order):
powers = itertools.product(range(order + 1), range(order + 1))
return [tup for tup in powers if sum(tup) <= order]
main()
您可以做的一件简单的事情就是将表面的透明度设置为低于散点图的值。请参见下面的示例,其中我在行 ax.plot_surface(xx, yy, zz, zorder=10,alpha=0.4)
.
中使用了等于 0.4 的透明度值
并且输出给出:
我试图通过绘制所有内容并旋转表面来检查表面行为相对于 3d 中的散点的任何奇怪之处,从而了解表面与我的数据点的拟合程度 space.
问题是当我旋转渲染来执行此操作时,绘图消失了。我怎样才能让剧情持续下去?
您可以使用以下代码进行复现 - 主要取自 Python 3D polynomial surface fit, order dependent.
的惊人答案import numpy as np
import scipy.linalg
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import itertools
def main():
# Generate Data...
numdata = 100
x = np.random.random(numdata)
y = np.random.random(numdata)
z = x**2 + y**2 + 3*x**3 + y + np.random.random(numdata)
# Fit a 3rd order, 2d polynomial
m = polyfit2d(x,y,z)
# Evaluate it on a grid...
nx, ny = 20, 20
xx, yy = np.meshgrid(np.linspace(x.min(), x.max(), nx),
np.linspace(y.min(), y.max(), ny))
zz = polyval2d(xx, yy, m)
# Plot
#plt.imshow(zz, extent=(x.min(), y.max(), x.max(), y.min()))
#plt.scatter(x, y, c=z)
#plt.show()
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, y, z, color='red', zorder=0)
ax.plot_surface(xx, yy, zz, zorder=10)
ax.set_xlabel('X data')
ax.set_ylabel('Y data')
ax.set_zlabel('Z data')
plt.show()
text = "filler"
def polyfit2d(x, y, z, order=4):
ncols = (order + 1)**2
G = np.zeros((x.size, ncols))
#ij = itertools.product(range(order+1), range(order+1))
ij = xy_powers(order)
for k, (i,j) in enumerate(ij):
G[:,k] = x**i * y**j
m, _, _, _ = np.linalg.lstsq(G, z)
return m
def polyval2d(x, y, m):
order = int(np.sqrt(len(m))) - 1
#ij = itertools.product(range(order+1), range(order+1))
ij = xy_powers(order)
z = np.zeros_like(x)
for a, (i,j) in zip(m, ij):
z += a * x**i * y**j
return z
def xy_powers(order):
powers = itertools.product(range(order + 1), range(order + 1))
return [tup for tup in powers if sum(tup) <= order]
main()
您可以做的一件简单的事情就是将表面的透明度设置为低于散点图的值。请参见下面的示例,其中我在行 ax.plot_surface(xx, yy, zz, zorder=10,alpha=0.4)
.
并且输出给出: