Skip to content

Instantly share code, notes, and snippets.

@AdamGagorik
Created December 1, 2016 16:03
Show Gist options
  • Save AdamGagorik/f89b489311dc3ec9e49e727f55731e2d to your computer and use it in GitHub Desktop.
Save AdamGagorik/f89b489311dc3ec9e49e727f55731e2d to your computer and use it in GitHub Desktop.
def create_subplots(n_row, n_col, cell_w=5.0, cell_h=5.0, sep_w=1.0, sep_v=1.0,
ls=1.0, rs=1.0, ts=1.0, bs=1.0):
"""
Create subplot grid using absolute widths.
Parameters:
n_row (int): rows in subplot grid
n_col (int): columns in subplot grid
cell_w (float): width of each cell
cell_h (float): height of each cell
sep_w (float): horizontal space between each subplot
sep_v (float): vertical space between each subplot
ls (float): left margin
rs (float): right margin
ts (float): top margin
bs (float): bottom margin
"""
tw = rs + ls + n_col * cell_w + (n_col - 1) * sep_w
th = bs + ts + n_row * cell_h + (n_row - 1) * sep_v
fl = ls / float(tw)
fr = (tw - rs) / float(tw)
fb = bs / float(th)
ft = (th - ts) / float(th)
if cell_w > 0.0:
ws = sep_w / cell_w
else:
ws = 0.0
if cell_h > 0.0:
hs = sep_v / cell_h
else:
hs = 0.0
fig, axes = plt.subplots(n_row, n_col, figsize=(tw, th))
plt.subplots_adjust(left=fl, right=fr, bottom=fb, top=ft, wspace=ws,
hspace=hs)
return fig, axes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment