Question

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?

Was it helpful?

Solution

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))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top