我有下图由1行2列的网格组成。我想要


减小右侧子图的高度(3D PREDICTION),以使棋盘平面看起来有点挤压,并显示更好的透视图。
在左侧子图的顶部添加一些边距(2D PREDICTION),以使标题与3D PREDICTION之一对齐


您知道如何执行此操作吗?

python - 降低Matplotlib中子图的高度-LMLPHP

这是输出上面图像的代码

import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.art3d as art3d
import numpy as np
from matplotlib.patches import Rectangle



# Create figure 1920x960
background_color = (0.3, 0.3, 0.3, 1.0) # Set background color to dark grey
fig = plt.figure(figsize=[32, 16], facecolor=background_color, edgecolor='none')
grid = gridspec.GridSpec(nrows=1, ncols=2, figure=fig)



# Create 2D visualization in first subplot
viz2D = fig.add_subplot(grid[0, 0])
viz2D.set_facecolor(background_color)
viz2D.set_title('2D PREDICTION', color='white', size='xx-large')

# Remove tick labels
viz2D.set_xticks([])
viz2D.set_yticks([])




# Create 3D visualization in second subplot
viz3D = fig.add_subplot(grid[0, 1], projection='3d')
viz3D.set_facecolor(background_color)
viz3D.set_title('3D PREDICTION', color='white', size='xx-large')
viz3D.grid(False) # Remove grid lines

# Set transparent planes
viz3D.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) # Left plane
viz3D.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) # Right plane
viz3D.w_zaxis.set_pane_color((0.6, 0.6, 0.6, 0.0)) # Horizontal plane

# Set transparent spines
viz3D.w_xaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
viz3D.w_yaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
viz3D.w_zaxis.line.set_color((1.0, 1.0, 1.0, 0.0))

# Remove tick labels
viz3D.set_xticks([])
viz3D.set_yticks([])
viz3D.set_zticks([])

# Define chessboard dimensions
RECT_SIZE_X = 0.1
RECT_SIZE_Y = 0.1
xlims = (-1, 1)
ylims = (-1, 1)
zlims = (0, 15)

# Draw chessboard on hortizontal plane
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='#666666')
        else:
            p = Rectangle([x_pos, y_pos], RECT_SIZE_X, RECT_SIZE_Y, color='#999999')
        viz3D.add_patch(p)
        art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")

viz3D.set(xlim=xlims, ylim=ylims, zlim=zlims)

# 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)
# viz3D.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, facecolor=fig.get_facecolor(), edgecolor='none') # Note these overwrite the params in plt.figure

最佳答案

在您的代码中,我更改了这一行

viz2D.set_title('2D PREDICTION', color='white', size='xx-large')




viz2D.title.set_text('2D PREDICTION')
viz2D.title.set_color('white')


对于viz3D也是如此。

关于3D视图,我用以下代码在相机上玩了一些:

viz3D.view_init(10, 30) # elevation, azimuthal


经过以上修改,我得到以下图:

python - 降低Matplotlib中子图的高度-LMLPHP

关于python - 降低Matplotlib中子图的高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59890982/

10-13 00:47