【问题标题】:How to make plots in normalized coordinates in python?如何在python中以标准化坐标绘制图?
【发布时间】:2015-04-07 13:30:37
【问题描述】:

我正在尝试在 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 和 subplots,但找不到让 plot_subset() 在标准化空间中工作的方法。任何帮助将不胜感激!

【问题讨论】:

标签: python matplotlib


【解决方案1】:

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

输出:

【讨论】:

  • 谢谢!正是我的想法。
猜你喜欢
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 2018-07-23
  • 2019-01-26
  • 1970-01-01
相关资源
最近更新 更多