1

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?

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
DanielPerez
  • 47
  • 2
  • 6
  • 1
    Possible duplicate of [Set size of subplot in matplotlib](https://stackoverflow.com/questions/41530975/set-size-of-subplot-in-matplotlib) – Dadep Jul 07 '17 at 20:27
  • Can you post your code ? – Dadep Jul 07 '17 at 20:28
  • 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 [mcve] of the issue. – ImportanceOfBeingErnest Jul 08 '17 at 08:55

1 Answers1

3

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()
rinkert
  • 6,593
  • 2
  • 12
  • 31
  • Happy to help, but just accept the answer and or upvote if it was helpfull – rinkert Jul 07 '17 at 22:02
  • 2
    Even so, it is not exactly what I was looking for, because the size and position of the subplots is relative. I find strange not to be possible to define arbitrarily the size and position of the subplots – DanielPerez Jul 07 '17 at 22:13