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

2024-09-28 21:53:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在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和subblot,但找不到一种使plot_subset()在规范化空间中工作的方法。任何帮助都将不胜感激!在


Tags: topos网格面板绘图plotdef绘制
1条回答
网友
1楼 · 发布于 2024-09-28 21:53:06

您可以使用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

相关问题 更多 >