降低Matplotlib中子图的高度 [英] Reduce height of subplot in Matplotlib

查看:104
本文介绍了降低Matplotlib中子图的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下图是由1行2列的网格组成的.我想

I have the following figure composed of grid with 1 row and 2 columns. I would like to

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

请问该怎么做?

这里是输出上面图像的代码

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

推荐答案

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

In your code, I changed the line

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

使用

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

viz3D也是如此.

关于3D视图,我用以下行的摄像头进行了一些播放:

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:

这篇关于降低Matplotlib中子图的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆