matplotlib-绘制具有相同大小的中心坐标的3D立方体

发布于 2025-02-04 14:47:29 字数 435 浏览 2 评论 0原文

我有一个空间数据集,其中具有特定尺寸(20 m)的立方体中心坐标,以及用于颜色映射的变量。

我想在3D中绘制这些立方体;但是,我找不到方法。我试图通过计算多维数据集的角坐标来实现体素代码,但在某个点之后就卡住了。

数据集看起来像这样:

      x       y      z     variable
0   14630   21750   4690    0.087
1   14630   21770   4690    0.046
2   14630   21790   4690    0.045
3   14630   21930   5290    0.1657
4   14630   21950   5270    0.1144

我不确定是否应该使用ax.voxels或poly3dcollection。因此,如果您有任何提示,我将不胜感激。

I have a spatial dataset with the center coordinates of cubes with a specific size (20 m), and a variable for color mapping.

I want to plot these cubes in 3D; however, I couldn't find a way to do it. I've tried to implement a voxel code by calculating the corner coordinates of the cubes but got stuck after some point.

The dataset looks like this:

      x       y      z     variable
0   14630   21750   4690    0.087
1   14630   21770   4690    0.046
2   14630   21790   4690    0.045
3   14630   21930   5290    0.1657
4   14630   21950   5270    0.1144

I'm not sure if I should be using ax.voxels or Poly3DCollection. So, if you have any tips, I'd appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水晶透心 2025-02-11 14:47:29

iiuc,只需修改这个出色的答案 @david_sd在a

评论/I.SSTATIC.NET/4NAXV.PNG“ rel =” nofollow noreferrer“>

绘制3D立方体的代码:

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

df = pd.DataFrame({"x": [14630, 14630, 14360, 14360, 14360], "y" : [21750, 21770, 21790, 21930, 21950], "z" : [4690, 4690, 4690, 5290, 5270]})

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


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

for i in df.index:
    x,y,z = get_cube()
    
    # Change the centroid of the cube from zero to values in data frame
    x = x*L + df.x[i]
    y = y*L + df.y[i]
    z = z*L + df.z[i]
    ax.plot_surface(x, y, z)
    ax.set_zlabel("z")

plt.xlabel("x")
plt.ylabel("y")
plt.show()

IIUC, simply modifying this brilliant answer pointed by @David_sd in a comment, you can get the following figure:

enter image description here

Code to plot 3D cubes:

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

df = pd.DataFrame({"x": [14630, 14630, 14360, 14360, 14360], "y" : [21750, 21770, 21790, 21930, 21950], "z" : [4690, 4690, 4690, 5290, 5270]})

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


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

for i in df.index:
    x,y,z = get_cube()
    
    # Change the centroid of the cube from zero to values in data frame
    x = x*L + df.x[i]
    y = y*L + df.y[i]
    z = z*L + df.z[i]
    ax.plot_surface(x, y, z)
    ax.set_zlabel("z")

plt.xlabel("x")
plt.ylabel("y")
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文