简体   繁体   English

如何在保持宽度相同的同时强制子图的纵横比?

[英]How to force aspect ratio of subplot while keeping the width identical?

I'm trying to create a plot with two subplots.我正在尝试创建一个带有两个子图的 plot。 1 column, 2 rows. 1 列,2 行。 Similar to the image but without the subplot to the right与图像类似,但右侧没有子图

How can I enforce one subplot to be square and the other one to have the same width without being square?如何强制一个子图为正方形,而另一个子图具有相同的宽度而不是正方形?

来自 seaborn 画廊

I tried gridspec without success:我尝试了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,:])

I also tried setting the aspect ratio for both subplots resulting in different widths the subplots:我还尝试为两个子图设置纵横比,从而导致子图的宽度不同:

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

I also tried... but this fixes the x range of the subplots to the same value.我也试过......但这会将子图的 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)

Thanks for any suggestions...感谢您的任何建议...

One working solution I could come up with following the suggestions of ImportanceOfBeingErnest:我可以按照 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)

Thank you!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM