Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Gridspec height_ratio issue

I'm very new to python and matplotlib and having trouble with adjusting the height of a 2 plot subplot window. I'm trying to make the subplots aligned vertically, with one directly on top of the other. The subplot on the bottom should be half the height as the one on top. If I try and use width_ratios I get exactly what I would expect (though it's not what I want). But when I use height_ratios I get both plots aligned horizontally with empty space below them both that is half the height of the subplots. The relevant portion of the code is below

pl.figure()

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

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

I'm sure I'm doing something very simple (and probably dumb) but to my newbie eyes I can't see it.

like image 451
KCook3824 Avatar asked Oct 28 '25 16:10

KCook3824


2 Answers

These two have a ratio 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

you can adjust rect_top and rect_bot bottom coordinates and heights to get the proportion and aspect you want. The following gives a fast method to change proportions just by modifying the value of ratio. This gives the proportion you expect (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]
like image 67
joaquin Avatar answered Oct 31 '25 06:10

joaquin


You switched rows and columns in your Gridspec statement:

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

works.

like image 32
tillsten Avatar answered Oct 31 '25 06:10

tillsten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!