简体   繁体   English

旋转 3d 表面 plot 时散点消失

[英]Scatter points are disappearing when rotating a 3d surface plot

I'm trying to get a feel for how well a surface fits my data points by graphing everything and rotating the surface around to check for any oddities in the surface behavior relative to the scattered point in 3d space.我试图通过绘制所有内容并旋转表面以检查表面行为相对于 3d 空间中的散点是否存在任何异常,从而了解表面与我的数据点的匹配程度。

The problem is that when I rotate the render to do this, the plots disappear.问题是当我旋转渲染来执行此操作时,绘图消失了。 How can I make the plots persist?我怎样才能使情节持续存在?

在此处输入图像描述

You can repro with the below code - mainly taken from the amazing answers at Python 3D polynomial surface fit, order dependent .您可以使用以下代码进行复制 - 主要取自Python 3D 多项式曲面拟合的惊人答案,顺序相关

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()

A simple thing you can do is setting the transparency of your surface to a lower value than your scatter plot.您可以做的一件简单的事情是将表面的透明度设置为低于散点图 plot 的值。 See example below where I used a transparency value equal to 0.4 with the line ax.plot_surface(xx, yy, zz, zorder=10,alpha=0.4) .请参见下面的示例,其中我在ax.plot_surface(xx, yy, zz, zorder=10,alpha=0.4)行中使用了等于 0.4 的透明度值。

And the output gives: output 给出:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM