【问题标题】:Scatter points are disappearing when rotating a 3d surface plot旋转 3d 曲面图时散点消失
【发布时间】:2022-01-27 08:37:15
【问题描述】:

我试图通过绘制所有图形并旋转表面以检查表面行为相对于 3d 空间中的分散点是否存在任何异常,从而了解表面与我的数据点的匹配程度。

问题是,当我旋转渲染来执行此操作时,绘图消失了。我怎样才能使情节持续存在?

您可以使用以下代码进行复制 - 主要取自 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()

【问题讨论】:

    标签: python matplotlib 3d surface


    【解决方案1】:

    您可以做的一件简单的事情是将曲面的透明度设置为低于散点图的值。请参阅下面的示例,其中我在行 ax.plot_surface(xx, yy, zz, zorder=10,alpha=0.4) 中使用了等于 0.4 的透明度值。

    输出给出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2019-01-27
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多