0

I am following this discussion to produce a plot in which report different data sets whose x-axis is the same, but y-axis has different scales. I can reproduce my plot, similar to this:

enter image description here

With some code:

# Twin the x-axis twice to make independent y-axes.
axes = [ax, ax.twinx(), ax.twinx()]

# Make some space on the right side for the extra y-axis.
fig.subplots_adjust(right=0.75)

# Move the last y-axis spine over to the right by 20% of the width of the axes
axes[-1].spines['right'].set_position(('axes', 1.2))

# To make the border of the right-most axis visible, we need to turn the frame
# on. This hides the other plots, however, so we need to turn its fill off.
axes[-1].set_frame_on(True)
axes[-1].patch.set_visible(False)

But it happens that, by side the figure, there is plenty of empty room, so that the figure is "squeezed" in 3/4 of the sheet, instead of being spread over the whole page.

Why does this happen? And what could be a possible way to avoid it?

Community
  • 1
  • 1
Py-ser
  • 1,860
  • 9
  • 32
  • 58
  • works fine for me under python 2.7.6 and matplotlib 1.3.0, no visible "sqeeze" : https://i.imgur.com/ASrVKM6.png – Louis Jun 05 '14 at 07:28
  • @Louis, yes this is only part of the code, probably something else I add is bringing the squeezing, for example I add an upper x-axis too to the plot. – Py-ser Jun 05 '14 at 07:33
  • I just copy-pasted the code from your link "this discussion". Does this original code works for you ? What matplotlib are you using ? Did you modify the code ? If yes, please post it ! – Louis Jun 05 '14 at 07:58

1 Answers1

1

Normally you would get this:

enter image description here

You may want to call fig.tight_layout() before calling plt.show().

enter image description here

You also want to fiddle with the axis offset (1.13 instead of 1.2):

axes[-1].spines['right'].set_position(('axes', 1.13))
nepix32
  • 3,012
  • 2
  • 14
  • 29