Python / Gridspec height_ratio问题

时间:2011-12-20 23:23:20

标签: python matplotlib

我对python和matplotlib很新,并且无法调整2个绘图子图窗口的高度。我试图使子图垂直对齐,一个直接在另一个上面。底部的子图应该是顶部的一半高度。如果我尝试使用width_ratios,我会得到我所期望的(虽然这不是我想要的)。但是当我使用height_ratios时,我得到的两个图都是水平对齐的,它们下方的空白区域都是子图的高度的一半。代码的相关部分位于

之下
pl.figure()

grid = gs.GridSpec(1, 2, height_ratios=[2,1])

ax1 = pl.subplot(grid[0])
ax2 = pl.subplot(grid[1])

我确定我做的事情非常简单(也许可能是愚蠢的),但对于我的新手眼睛,我看不到它。

2 个答案:

答案 0 :(得分:3)

这两者的比例为3:1:

import matplotlib.pyplot as plt

#          left  bott width height
rect_bot = [0.1, 0.1,  0.8,  0.6]
rect_top = [0.1, 0.75, 0.8,  0.2]

plt.figure(1, figsize=(8,8))

bot = plt.axes(rect_bot)
top = plt.axes(rect_top)

bot.plot([3, 2, 60, 4, 7])
top.plot([2, 3, 5, 3, 2, 4, 6])

plt.show()

enter image description here

您可以调整rect_toprect_bot底部坐标和高度,以获得所需的比例和方位。以下提供了一种通过修改ratio的值来更改比例的快速方法。这给出了你期望的比例(1:2):

left, width = 0.1, 0.8

ratio = 2
border = 0.1
total = 1

bh = (total - 3 * border) / (total + ratio)
th = ratio * bh
tb = bh + 2 * border

#           left  bott    width    height
rect_bot = [left, border,    w,      bh]
rect_top = [left, tb,    width,      th]

答案 1 :(得分:2)

您在Gridspec声明中切换了行和列:

grid = gs.GridSpec(2,1 , height_ratios=[2,1])

作品。

相关问题