如何在python中的标准化坐标中绘制图?

时间:2015-04-06 17:40:00

标签: python matplotlib

我正在尝试在2x2或1x4网格中制作四组图。然后每组具有三个面板,例如,散布图,其侧面具有x轴和y轴的直方图。

我不是为所有12个图设置轴,而是将画布分成4个部分,然后将每个部分分开。例如,

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

我已经尝试了GridSpec和子图,但找不到使plot_subset()在规范化空间中工作的方法。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

您可以使用BboxTransformTo()执行此操作:

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)))

输出:

enter image description here