我是 python 和 matplotlib 的新手,在调整 2 plot 子图窗口的高度时遇到了问题。我正在尝试使子图垂直对齐,一个直接在另一个之上。底部的子图应该是顶部高度的一半。如果我尝试使用 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])
我确定我正在做一些非常简单(并且可能很愚蠢)的事情,但对于我的新手来说我看不到它。
这两个有 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()
您可以调整 rect_top
和 rect_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]
我是一名优秀的程序员,十分优秀!