如何在 matplotlib/python 中绘制 3D 块?

问题描述 投票:0回答:1

我正在尝试绘制与此类似的 3D 块:example picture

如何绘制块并控制其大小以获得与示例类似的结果?

我尝试过不同的事情:

  1. 具有透明度和 cmap 的基本散射
  2. 体素绘图

我对我的结果不是很满意,它看起来不如我分享的示例那么丰富。

python matplotlib plot 3d
1个回答
0
投票

这就是你想要的效果吗?

示例数据和代码:

import matplotlib.pyplot as plt
import numpy as np

x_grid, y_grid = np.meshgrid(
    np.linspace(0, np.pi, num=100), np.linspace(0, np.pi, num=100)
)

substrate = np.random.uniform(size=(100, 100))
structure = np.sin(x_grid) + np.cos(y_grid)
structure = (structure + 1) / 3

ax = plt.figure(figsize=(5, 5)).add_subplot(projection='3d')
s = ax.scatter(
    x_grid, y_grid, structure, depthshade=False, alpha=1,
    c=structure, edgecolor='none', marker='o', cmap='jet')
ax.scatter(
    x_grid, y_grid, substrate, s=substrate.T * 10, #depthshade=False,
    color='blue', edgecolor='none', marker='o', alpha=0.2
)
plt.colorbar(mappable=s, shrink=0.5)
ax.set_box_aspect(aspect=None, zoom=0.9)
© www.soinside.com 2019 - 2024. All rights reserved.