0
votes

I'm trying to create a plot with two subplots. 1 column, 2 rows. 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?

from the seaborn gallery

I tried gridspec without success:

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.

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...

1
Several options. (1) Setting the aspect for both subplots, (2) Use a divider via make_axes_locatable, (3) Adjust the subplot parameters (or gridspec parameters) such that both axes are confined and one is square. I think all those exist as answers on SO already. - ImportanceOfBeingErnest

1 Answers

0
votes

One working solution I could come up with following the suggestions of 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!