【问题标题】:Plot image on plane in Matplotlib mplot3d在 Matplotlib mplot3d 中的平面上绘制图像
【发布时间】:2020-01-22 11:10:25
【问题描述】:

我有一个使用 Matplotlib 创建的 3D 场景,我想在下图中的底部平面上应用一个类似棋盘的图案。您知道如何实现这一目标吗?

这是创建此图像的代码:

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

# Create figure
plt.style.use('dark_background') # Dark theme
fig = plt.figure(frameon=False)
ax = fig.add_subplot(111, projection='3d')

# Make panes transparent
ax.xaxis.pane.fill = False # Left pane
ax.yaxis.pane.fill = False # Right pane
# ax.zaxis.pane.fill = False # Bottom pane

# Remove grid lines
ax.grid(False)

# Remove tick labels
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])

# Random data to illustrate
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens')

# Print chart
file_path = 'charts/3d.png'
fig.savefig(file_path, bbox_inches='tight', pad_inches=0.05, transparent=True)

【问题讨论】:

    标签: python matplotlib mplot3d


    【解决方案1】:

    您可以生成一个矩形网格,然后使用 mpl_toolkits.mplot3d.art3d 中的 pathpatch_2d_to_3d 函数将其插入到 3d 场景中:

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    from matplotlib.patches import Rectangle
    import mpl_toolkits.mplot3d.art3d as art3d
    
    # Create figure
    plt.style.use('dark_background') # Dark theme
    fig = plt.figure(frameon=False)
    ax = fig.add_subplot(111, projection='3d')
    
    # Make planes transparent
    ax.xaxis.pane.fill = False # Left plane
    ax.yaxis.pane.fill = False # Right plane
    # ax.zaxis.pane.fill = False # Horizontal plane
    
    # Remove grid lines
    ax.grid(False)
    
    # Remove tick labels
    ax.set_xticklabels([])
    ax.set_yticklabels([])
    ax.set_zticklabels([])
    
    # Draw chessboard on hortizontal plane
    for x_index, x in enumerate(np.arange(-1, 1.1, 0.2)):
        for y_index, y in enumerate(np.arange(-1, 1.1, 0.2)):
            if (x_index+y_index)%2:
                p = Rectangle([x,y], 0.2, 0.2)
                ax.add_patch(p)
                art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")
    
    ax.set(xlim=(-1,1.1), ylim=(-1,1.2), zlim=(0,15))
    
    # Random data to illustrate
    zdata = 15 * np.random.random(100)
    xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
    ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
    ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens')
    
    # Print chart
    file_path = 'charts/3d.png'
    fig.savefig(file_path, bbox_inches='tight', pad_inches=0.05, transparent=True)
    


    编辑:更干净的版本

    RECT_SIZE_X = 0.2
    RECT_SIZE_Y = 0.2
    xlims = (-1, 1)
    ylims = (-1, 1)
    
    for x_index, x_pos in enumerate(np.arange(xlims[0], xlims[1], RECT_SIZE_X)):
        for y_index, y_pos in enumerate(np.arange(ylims[0], ylims[1], RECT_SIZE_Y)):
            if (x_index+y_index)%2:
                p = Rectangle([x_pos, y_pos], RECT_SIZE_X, RECT_SIZE_Y, color='orange')
            else:
                p = Rectangle([x_pos, y_pos], RECT_SIZE_X, RECT_SIZE_Y, color='gray')
            ax.add_patch(p)
            art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")
    
    ax.set(xlim=xlims, ylim=ylims, zlim=(0,15))
    # Transparent spines
    ax.w_xaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
    ax.w_yaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
    ax.w_zaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
    
    # Transparent panes
    ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
    ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
    ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
    
    # No ticks
    ax.set_xticks([]) 
    ax.set_yticks([]) 
    ax.set_zticks([])
    

    【讨论】:

    • 漂亮!非常感谢,我试试看!
    • 似乎最后一行在您的代码中触发错误 sn-p ax.set(xlim=(-1,1.1), ylim=(-1,1.2), zlim=(0,15)) 返回SyntaxError: invalid syntax
    • 我发现了这个问题,在 for 循环修复它之后添加了一个额外的行
    • 既然你似乎对 matplotlib 很有天赋,你知道如何做这个吗? stackoverflow.com/questions/59857203/…
    • 我承认我并没有考虑太多,我只是做了一个棋盘格,然后调整了 x 和 y 边缘以使其工作。我添加了一个更简洁的代码版本,它应该更不言自明,但如果您有疑问,请询问。附言。如果你把飞机留在那儿,我不完全理解的轴限制有一点不匹配,可能需要手工进行临时调整
    猜你喜欢
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    相关资源
    最近更新 更多