首页 > 解决方案 > 使用 add_collection3d 绘制 Poly3DCollection

问题描述

我有相同大小的树数组,表示空间中点的球坐标。我想将它们绘制成笛卡尔坐标。我正在尝试生成一个表面,我需要使用该add_collection3d方法而不是plot_surface因为我的数组的尺寸。原始数组在球坐标中具有不同的长度,并且转换为笛卡尔坐标不是线性的。

一个简化的例子如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LightSource
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from mpl_toolkits.mplot3d import Axes3D


phi_rad = np.linspace(0,360, 10)/180.0*np.pi
theta_rad = np.linspace(0,360, 10)/180.0*np.pi # cos(theta)
counts_str = np.linspace(0, 100, 10) # counts

# convertion to cartesian coordinates 1D arrays
x = counts_str * np.sin(theta_rad) * np.cos(phi_rad)
y = counts_str * np.sin(theta_rad) * np.sin(phi_rad)
z_str = counts_str * np.cos(theta_rad)

verts = [list(zip(x, y, z_str))]

fig = plt.figure()
ax = Axes3D(fig)

ax.add_collection3d(Poly3DCollection(verts, cmap="hot", alpha=0.9))
ls = LightSource(azdeg=225.0, altdeg=45.0)

ax.set_xlim3d(x.min(), x.max())
ax.set_ylim3d(y.min(), y.max())
ax.set_zlim3d(z_str.min(), z_str.max())

plt.show()

我想应用 acmap和 a LightSource(不影响绘图),以及 aantialiased因为在我的真实数据中 z 是一个包含 20000 个元素的数组。

期待听到您的集体智慧!

标签: matplotlib3dlight

解决方案


解决方案:重塑所有三个向量并使用曲面图!

从三个 1D 数组创建 3D 曲面图


推荐阅读