Python/Gridspec height_ratio 问题

标签 python matplotlib

我是 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()

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]

关于Python/Gridspec height_ratio 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8583281/

相关文章:

散点图中的 Python Matplotlib 线

python - Django:进行数据库查询时出现UnboundLocalError

分配的 Python 对齐方式(样式)

python - Django 模板 : load numbered img to html

python - 将 Matplotlib 图作为 QGraphicsItem/放入 QGraphicsView

python - 如何在鼠标悬停事件发生时更改饼图切片的颜色

python-3.x - 使用 matplotlib 将 3D 散点图动画化为 gif 最终为空

python - 如何使用 pytest 在 Django 中测试重定向?

python - PyQt如何使工具栏按钮显示为按下

python类默认值继承