0

I am trying to make four sets of plots in a 2x2 or 1x4 grid. Each set then has three more panels, say, a scatter plot with histograms of the x- and y-axes on the sides.

Instead of setting the axes for all 12 plots, I'd like to divide my canvas into 4 parts, and then divide each one individually. For example,

def plot_subset():
    # these coords are normalized to this subset of plots
    pos_axScatter=[0.10, 0.10, 0.65, 0.65]
    pos_axHistx = [0.10, 0.75, 0.65, 0.20]
    pos_axHisty = [0.75, 0.10, 0.20, 0.20]

    axScatter = plt.axes(pos_axScatter)
    axHistx = plt.axes(pos_axHistx)
    axHisty = plt.axes(pos_axHisty)

def main():
    # need to divide the canvas to a 2x2 grid
    plot_subset(1)
    plot_subset(2)
    plot_subset(3)
    plot_subset(4)

    plt.show()

I have tried GridSpec and subplots but cannot find a way to make plot_subset() work in the normalized space. Any help would be much appreciated!

saud
  • 773
  • 1
  • 7
  • 20
  • Add a little data and turn this into a full working example so you can explain what's going wrong. Right now it looks like you should just follow http://stackoverflow.com/questions/13612610/plotting-autoscaled-subplots-with-fixed-limits-in-matplotlib and maybe set the axis limits afterwards. – cphlewis Apr 06 '15 at 20:15
  • I don't really understand your question I think http://matplotlib.org/users/gridspec.html#adjust-gridspec-layout may be what you want? – tacaswell Apr 06 '15 at 23:29

1 Answers1

2

You can use BboxTransformTo() to do this:

from matplotlib import transforms

fig = plt.figure(figsize=(16, 4))

fig.subplots_adjust(0.05, 0.05, 0.95, 0.95, 0.04, 0.04)

gs1 = plt.GridSpec(1, 4)
gs2 = plt.GridSpec(4, 4)

for i in range(4):
    bbox = gs1[0, i].get_position(fig)
    t = transforms.BboxTransformTo(bbox)

    fig.add_axes(t.transform_bbox(gs2[:3, :3].get_position(fig)))
    fig.add_axes(t.transform_bbox(gs2[3, :3].get_position(fig)))
    fig.add_axes(t.transform_bbox(gs2[:3, 3].get_position(fig)))

the output:

enter image description here

HYRY
  • 94,853
  • 25
  • 187
  • 187