【问题标题】:How to force aspect ratio of subplot while keeping the width identical?如何在保持宽度相同的同时强制子图的纵横比?
【发布时间】:2020-02-09 09:40:22
【问题描述】:

我正在尝试创建一个包含两个子图的图。 1 列,2 行。 与图片类似,但右侧没有子图

如何强制一个子图是正方形的,而另一个子图具有相同的宽度而不是正方形?

我尝试了gridspec但没有成功:

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure(constrained_layout=True)

gs = GridSpec(nrows=4, ncols=3, height_ratios=[1, 1, 1, 1], figure=fig)
ax1 = fig.add_subplot(gs[:-1,:])
ax2 = fig.add_subplot(gs[-1,:])

我还尝试为两个子图设置纵横比,从而导致子图的宽度不同:

fig, axs = plt.subplots(2)
axs[0].set_aspect(1)
axs[1].set_aspect(2)

我也尝试过...但这会将子图的 x 范围固定为相同的值。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid

F = plt.figure()
grid = ImageGrid(F, 111,
                 nrows_ncols=(2, 1),
                 axes_pad=0.1,
                 )

grid[1].set_aspect(.4)

感谢您的任何建议...

【问题讨论】:

  • 几个选项。 (1) 设置两个子图的纵横比,(2) 通过make_axes_locatable 使用分隔线,(3) 调整子图参数(或 gridspec 参数),使两个轴都被限制,一个是正方形。我认为所有这些都作为 SO 的答案存在。

标签: python matplotlib subplot


【解决方案1】:

我可以按照 ImportanceOfBeingErnest 的建议提出一个可行的解决方案:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable

fig = plt.figure(figsize=(10,12))
axScatter = plt.subplot(111)
axScatter.set_aspect(1.)

divider = make_axes_locatable(axScatter)
axHistx = divider.append_axes("bottom", size=.8, pad=0.2)

谢谢!

【讨论】:

    猜你喜欢
    • 2021-06-23
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多