gpt4 book ai didi

python - 降低 Matplotlib 中子图的高度

转载 作者:行者123 更新时间:2023-12-01 00:07:11 28 4
gpt4 key购买 nike

我有下图,由 1 行 2 列的网格组成。我愿意

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

知道如何做到这一点吗?

enter image description here

这是输出上面图像的代码

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 View ,我用相机玩了一下,代码如下:

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

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

enter image description here

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com