【问题标题】:Matplotlib How to get the gap and whitespace distance between subplots (axes)Matplotlib 如何获取子图(轴)之间的间隙和空白距离
【发布时间】:2020-01-08 21:44:53
【问题描述】:

请问是否有人知道如何打印子图(轴)之间的空白距离?我有我绘制两个网格的代码,你能帮我弄清楚如何为白色空间打印这些参数吗?

grid_data = np.array([[[-0.1779874 , -0.90335705, -0.31157705,  0.77770067],
        [ 0.93698288,  0.79215241,  0.10155888,  0.96101718],
        [ 0.72994894, -0.83939131,  0.24713443,  0.74839211],
        [ 0.10039462, -0.95778299,  0.43554077,  0.61927077]],

       [[ 0.52294259, -0.0247383 ,  0.23717517, -0.0857769 ],
        [-0.43539246,  0.28503173, -0.39443502, -0.1478289 ],
        [-0.2327904 , -0.08339054,  0.33072907,  0.74634504],
        [-0.524284  , -0.72919194, -0.61543159,  0.17086563]]])
num_cols = 2
num_rows = 1
fig_size_h = 42/32*num_cols
fig_size_v = 14/10*num_rows
fig, axes = plt.subplots(nrows = num_rows, ncols = num_cols, figsize = (fig_size_h, fig_size_v))
for i in range(num_cols):
    c = axes[i].pcolormesh(grid_data[i], cmap = 'RdBu_r', vmin = -1., vmax = 1.)
# Add a color bar
cb_ax = fig.add_axes([0.91, 0.13, 0.01, 0.75])
fig.colorbar(c, cax = cb_ax)
plt.show()

非常感谢您。

【问题讨论】:

  • 我对 matplotlib 会自动更改的默认空白胸肌有一种感觉,所以我想知道如何在 Python 中逐字打印这些数字。
  • 例如fig.subplotpars.leftleft 的值
  • @ImportanceOfBeingErnest 谢谢。你知道如何打印它自己的子图(轴)的大小吗?
  • 坐标区大小axw 由这些参数决定。 right-left = axw*(n+(n-1)*wspace),高度方向类似。
  • 但是,请注意,您可以通过ax.get_position() 直接获取坐标轴位置;所以这可能对许多情况更有帮助。

标签: python matplotlib visualization graph-visualization


【解决方案1】:

所有这些参数都可以使用这个来调整:

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.subplots_adjust.html

还列出了默认值

【讨论】:

  • 子图本身的大小是多少?
  • 顺便问一下,你知道访问这些数字的代码是什么,就像我们可以在 Python 中直接打印这些当前尺寸一样吗?