如何在 python 中以归一化坐标作图?
How to make plots in normalized coordinates in python?
我正在尝试在 2x2 或 1x4 网格中制作四组绘图。然后每组都有另外三个面板,比如说,一个散点图,两侧是 x 轴和 y 轴的直方图。
我不想为所有 12 个地块设置坐标轴,而是想将 canvas 分成 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() 在规范化 space 中工作的方法。任何帮助将不胜感激!
您可以使用 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)))
输出:
我正在尝试在 2x2 或 1x4 网格中制作四组绘图。然后每组都有另外三个面板,比如说,一个散点图,两侧是 x 轴和 y 轴的直方图。
我不想为所有 12 个地块设置坐标轴,而是想将 canvas 分成 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() 在规范化 space 中工作的方法。任何帮助将不胜感激!
您可以使用 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)))
输出: