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!

2

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Exactly what I was thinking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.