简体   繁体   English

如何在3D曲面图中绘制x,y,z的Numpy数组?

[英]How can I plot a numpy array of x, y, z in 3D surface plot?

I have reviewed both of these threads , but am still struggling to make a 3D surface plot from a numpy array of x, y, z coordinates. 我已经回顾了这两个 线程 ,但是仍在努力从x, y, z坐标的numpy数组制作3D表面图。

My array looks like this: 我的数组如下所示:

>>> points
array([[ 322697.1875    , 3663966.5       ,  -30000.        ],
       [ 325054.34375   , 3663966.5       ,  -30000.        ],
       [ 325054.34375   , 3665679.5       ,  -30000.        ],
       [ 322697.1875    , 3665679.5       ,  -30000.        ],
       [ 322697.1875    , 3663966.5       ,  -27703.12304688],
       [ 325054.34375   , 3663966.5       ,  -27703.15429688],
       [ 325054.34375   , 3665679.5       ,  -27703.70703125],
       [ 322697.1875    , 3665679.5       ,  -27703.67382812]])

ax.plot_surface accepts x, y, z points so I convert the above array into separate pieces below: ax.plot_surface接受x, y, z点,因此我将上面的数组转换为下面的单独部分:

x = points[:, 0]
y = points[:, 1]
z = points[:, 2]

I then put it into a meshgrid for passing into ax.plot_surface() : 然后,将其放入网状网格中,以传递到ax.plot_surface()

import numpy as np

X, Y, Z = np.meshgrid(x, y, z)

And then try to plot: 然后尝试绘制:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(16,10))
ax = plt.axes(projection = '3d')
ax.plot_surface(X, Y, Z, alpha=0.5)
plt.show()

When I run this I receive an error: rows, cols = Z.shape ValueError: too many values to unpack (expected 2) . 当我运行它时,我收到一个错误: rows, cols = Z.shape ValueError: too many values to unpack (expected 2)

I'm not sure where to go with this now, I don't expect the answer but a push in the correct direction would be great. 我不知道现在该去哪里,我不希望得到答案,但是朝正确方向前进将是巨大的。

I would like the output to be similar in appearance to this but with my data: 我希望输出在外观上与此类似,但有我的数据: 在此处输入图片说明

UPDATE: If I do not include z in the meshgrid , but only x and y , I get this output when I run ax.plot_surface(X, Y, z, alpha=0.5) : 更新:如果我不在meshgrid包含z ,而仅包含xy ,则在运行ax.plot_surface(X, Y, z, alpha=0.5)时得到以下输出: 在此处输入图片说明

This is really close, but I want all the sides to be filled in. Only one is showing as filled in. I've added the point coordinates to show the boundaries. 这确实很近,但是我希望所有侧面都被填充。只有一个侧面显示为填充。我添加了点坐标来显示边界。 I feel like it has something to do with the meshgrid that I'm creating. 我觉得这与我正在创建的meshgrid有关。 Here is the output of X, Y : 这是X, Y的输出:

>>> X, Y = np.meshgrid(x, y)
(array([[322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
        322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
       [322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
        322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
       [322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
        322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
       [322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
        322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
       [322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
        322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
       [322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
        322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
       [322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
        322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
       [322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
        322697.1875 , 325054.34375, 325054.34375, 322697.1875 ]]), array([[3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5,
        3663966.5, 3663966.5],
       [3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5,
        3663966.5, 3663966.5],
       [3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5,
        3665679.5, 3665679.5],
       [3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5,
        3665679.5, 3665679.5],
       [3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5,
        3663966.5, 3663966.5],
       [3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5,
        3663966.5, 3663966.5],
       [3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5,
        3665679.5, 3665679.5],
       [3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5,
        3665679.5, 3665679.5]]))

If I just take x, y unique values I get an error thrown: 如果仅取x,y唯一值,则会抛出错误:

x = np.unique(x)
y = np.unique(y)

>>> x
array([322697.1875 , 325054.34375])
>>> y
array([3663966.5, 3665679.5])

X, Y = np.meshgrid(x, y)
>>> X, Y
(array([[322697.1875 , 325054.34375],
       [322697.1875 , 325054.34375]]), array([[3663966.5, 3663966.5],
       [3665679.5, 3665679.5]]))

>>> ax.plot_surface(X, Y, z, alpha=0.5)
Traceback (most recent call last):
  File "<pyshell#61>", line 1, in <module>
    ax.plot_surface(X, Y, z, alpha=0.5)
  File "/Users/NaN/anaconda/envs/py36/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 1586, in plot_surface
    X, Y, Z = np.broadcast_arrays(X, Y, Z)
  File "/Users/NaN/anaconda/envs/py36/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 259, in broadcast_arrays
    shape = _broadcast_shape(*args)
  File "/Users/NaN/anaconda/envs/py36/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 193, in _broadcast_shape
    b = np.broadcast(*args[:32])
ValueError: shape mismatch: objects cannot be broadcast to a single shape

The arrays x, y, z need to be parametrized in two dimensions. 数组x,y,z需要二维设置参数。 One way of doing this is to use spherical coordinates as eg in Plot surfaces on a cube . 一种方法是使用球坐标,例如在立方体的绘图表面中

The remaining task is to distill the unique coordinates from the input data. 剩下的任务是从输入数据中提取唯一坐标。 I'm assuming here that there are only 2 distinct values per dimension. 我在这里假设每个维度只有2个不同的值。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def get_cube():   
    phi = np.arange(1,10,2)*np.pi/4
    Phi, Theta = np.meshgrid(phi, phi)

    x = np.cos(Phi)*np.sin(Theta)
    y = np.sin(Phi)*np.sin(Theta)
    z = np.cos(Theta)/np.sqrt(2)
    return x,y,z


points = np.array([[ 322697.1875    , 3663966.5       ,  -30000. ],
                   [ 325054.34375   , 3663966.5       ,  -30000. ],
                   [ 325054.34375   , 3665679.5       ,  -30000. ],
                   [ 322697.1875    , 3665679.5       ,  -30000. ],
                   [ 322697.1875    , 3663966.5       ,  -27703.12],
                   [ 325054.34375   , 3663966.5       ,  -27703.12],
                   [ 325054.34375   , 3665679.5       ,  -27703.12],
                   [ 322697.1875    , 3665679.5       ,  -27703.12]])

ux = np.unique(points[:,0])
uy = np.unique(points[:,1])
uz = np.unique(points[:,2])

x,y,z = get_cube()
offset = lambda X, o: o[0] + (X+.5)*np.diff(o)[0]


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(offset(x, ux), offset(y, uy), offset(z, uz))

plt.show()

在此处输入图片说明

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

相关问题 如何从 Plotly 中的 X、Y、Z 数组绘制 3D 表面? - How to plot a 3D surface from X,Y,Z array in Plotly? 给定 x,y,z 坐标的数据,我将如何在 Matplotlib 中制作 3D 表面 plot? - How would I make a 3D surface plot in Matplotlib given this data of x,y,z coordinates? 参数化3D曲面图,颜色取决于(x,y,z) - Parametric 3D Surface Plot with color depending on (x,y,z) 当Z是Python中的列表列表时,如何用X,Y,Z绘制3D曲面? - How to plot 3D surface with X, Y, Z when Z is a list of list in Python? 如何在mplot3d上绘制表面以获取(x,y,z)=(0,0,1),(0,0,2)之类的数据 - How can I plot surface on mplot3d for data like (x,y,z)=(0,0,1),(0,0,2) 如何获得轮廓 plot 和 3D plot 使用 z1587383CF8C21507D06FB 的 zDDDA 和 a set5ZEF3 列的起始点来表示? - How can I obtain a contour plot and a 3D plot using Matplotlib starting from a set of 3 columns representing x,y and z points? 如何使用python绘制numpy数组的3d曲面? - How to use python to plot a 3d surface of a numpy array? 3D表面图,其中z是采用由x和y形成的向量的函数 - 3D Surface Plot where z is a function that takes a vector formed from x and y 在python中从{x,y,z} - 散射数据绘制3D表面 - Plot a 3D surface from {x,y,z}-scatter data in python (python) 绘制 3d 表面,颜色图为第 4 维,x,y,z 的函数 - (python) plot 3d surface with colormap as 4th dimension, function of x,y,z
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM