亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Matplotlib mplot3d 中的平面上繪制圖像

在 Matplotlib mplot3d 中的平面上繪制圖像

慕萊塢森 2022-07-05 17:44:12
我有一個使用 Matplotlib 創建的 3D 場景,我想在下圖中的底部平面上應用一個類似棋盤的圖案。您知道如何實現這一目標嗎?這是創建此圖像的代碼:import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D# Create figureplt.style.use('dark_background') # Dark themefig = plt.figure(frameon=False)ax = fig.add_subplot(111, projection='3d')# Make panes transparentax.xaxis.pane.fill = False # Left paneax.yaxis.pane.fill = False # Right pane# ax.zaxis.pane.fill = False # Bottom pane# Remove grid linesax.grid(False)# Remove tick labelsax.set_xticklabels([])ax.set_yticklabels([])ax.set_zticklabels([])# Random data to illustratezdata = 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 chartfile_path = 'charts/3d.png'fig.savefig(file_path, bbox_inches='tight', pad_inches=0.05, transparent=True)
查看完整描述

1 回答

?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

您可以生成矩形網格并使用mpl_toolkits.mplot3d.art3dpathpatch_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)

http://img1.sycdn.imooc.com//62c40814000172ba03460227.jpg

編輯:更清潔的版本


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([])

http://img1.sycdn.imooc.com//62c408230001d09c03430226.jpg

查看完整回答
反對 回復 2022-07-05
  • 1 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號