我有一個空間資料集,其中包含具有特定大小(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。所以,如果您有任何提示,我將不勝感激。
uj5u.com熱心網友回復:
IIUC,簡單修改@David_sd在
繪制 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/487627.html
標籤:Python 熊猫 麻木的 matplotlib
