1
votes

I'm bothering you with a question about subplot sizes in matplotlib. I need to create a figure of a fixed size composed of 3 subplots in a single row. For 'editorial reasons' I need fix the size of the figure But I also want to fix the sizes and positions of the subplots without affecting the figure size (the third subplot must be narrower than the first two).

I tried using GridSpec without success. I also tried fixing the figure size with "figsize" and using add_axes for the subplots but, depending of the relative size of the subplots, the overall size of the figure and the subplots changes.

When using gnuplot one can use "set origin" and "set size" for the subplots. Us there something similar in matplotlib?

1
Possible duplicate of Set size of subplot in matplotlibDadep
Can you post your code ?Dadep
The figure size is not changed by adding any axes or by changing their size. There is no easy option to position the axes in absolute coordinates, but of course this can be calculated from the figure size and the relative coordinates. In any case, if you need help you need to show a minimal reproducible example of the issue.ImportanceOfBeingErnest

1 Answers

2
votes

Something like this? Changing the width ratio will change the sizes of the individual subplots.

fig, [ax1, ax2, ax3] = plt.subplots(1,3, gridspec_kw = {'width_ratios':[3, 2, 1]}, figsize=(10,10))

plt.show()

If you want to have some more control over the sizes you could also use Axes. It is still relative, but now in fractions of the entire figure size.

import matplotlib.pyplot as plt

# use plt.Axes(figure, [left, bottom, width, height])
# where each value in the frame is between 0 and 1

# left
figure = plt.figure(figsize=(10,3))
ax1 = plt.Axes(figure, [.1, .1, .25, .80])
figure.add_axes(ax1)
ax1.plot([1, 2, 3], [1, 2, 3])

# middle
ax2 = plt.Axes(figure, [.4, .1, .25, .80])
figure.add_axes(ax2)
ax2.plot([1, 2, 3], [1, 2, 3])

# right
ax3= plt.Axes(figure, [.7, .1, .25, .80])
figure.add_axes(ax3)
ax3.plot([1, 2, 3], [1, 2, 3])

plt.show()