问题
I have the following figure composed of grid with 1 row and 2 columns. I would like to
- reduce the height of the subplot on the right side (3D PREDICTION) so that the chessboard plane looks a little bit squeezed and shows a better perspective.
- add some margin at the top of the subplot on the left side (2D PREDICTION) so that the title is aligned with the one of 3D PREDICTION
Any idea how to do this please?
Here is the code to output the image above
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
回答1:
In your code, I changed the line
viz2D.set_title('2D PREDICTION', color='white', size='xx-large')
with
viz2D.title.set_text('2D PREDICTION')
viz2D.title.set_color('white')
and similarly for viz3D as well.
Regarding the 3D view, I played a bit with camera with following line:
viz3D.view_init(10, 30) # elevation, azimuthal
With above modifications, I get the following plot:
来源:https://stackoverflow.com/questions/59890982/reduce-height-of-subplot-in-matplotlib