Skip to content

tight_layout + lots of subplots + long ylabels inverts yaxis #8062

Closed

Description

@NelleV

When plotting a lot of subplots (here 6), with long y-labels and tight_layout, the y-axis is reverted.
In this example, it is particularly striking as I am plotting histograms.

import matplotlib.pyplot as plt
import numpy as np

alphas = [0.001, 0.1, 1, 10, 1000, 10000]
X = np.random.randn(len(alphas), 1000)

fig, axes = plt.subplots(nrows=len(alphas),  ncols=2)
fig.set_tight_layout(True)
for i, alpha in enumerate(alphas):
    ax = axes[i, 0]
    ax.hist(X[i])
    ax.set_xlabel("Number of features", fontweight="bold")
    ax.set_ylabel("Number of genes", fontweight="bold")

    ax = axes[i, 1]
    ax.plot(sorted(X[i]))
    ax.set_ylabel("Number of genes", fontweight="bold")

mpl_bug

Expected outcome

Removing the tight layout, the labels collapse, but at least the plots are oriented the correct way.

mpl_bug_wo_tight_layout

Matplotlib should either raise a proper error raise a proper warning, or plot the histograms in the proper order.

Matplotlib version

Matplotlib 2.0.0

Activity

  1. changed the title [-]tight_layout + lots of subplots + long ylabels reverts yaxis[/-] [+]tight_layout + lots of subplots + long ylabels inverts yaxis[/+] on Feb 10, 2017
  2. anntzer commented on Feb 10, 2017

    @anntzer
    Contributor

    Something like

    diff --git a/lib/matplotlib/tight_layout.py b/lib/matplotlib/tight_layout.py
    index 1f61d95e9..85f8a26d0 100644
    --- a/lib/matplotlib/tight_layout.py
    +++ b/lib/matplotlib/tight_layout.py
    @@ -199,7 +199,8 @@ def auto_adjust_subplotpars(fig, renderer,
         if rows > 1:
             vspace = (max(sum(s) for s in vspaces[cols:-cols])
                       + vpad_inches / fig_height_inch)
    -        v_axes = (1 - margin_top - margin_bottom - vspace * (rows - 1)) / rows
    +        v_axes = max((1 - margin_top - margin_bottom - vspace * (rows - 1)) / rows,
    +                     0.01)
             kwargs["hspace"] = vspace / v_axes
    
         return kwargs

    (and likewise for vspace) "fixes" the issue but the subplots are still tiny and squished (fundamentally there's just not enough space) so as @NelleV suggested an error may be better.

    (it's max(..., 0.01) and not max(..., 0) because matplotlib unsurprisingly doesn't like zero-sized axes...)

  3. added this to the milestone on Aug 10, 2017
  4. amyspark commented on Mar 30, 2018

    @amyspark

    I've stumbled onto this when plotting a 3x7 gridded figure in A4 paper. Has this been pushed?
    (Matplotlib 2.2.2)

  5. QuLogic commented on Mar 30, 2018

    @QuLogic
    Member

    @jklymak this looks like #4413 on the opposite axis.

  6. ImportanceOfBeingErnest commented on Mar 30, 2018

    @ImportanceOfBeingErnest
    Member

    @amyspark This happens if the title or the axes labels are too long. There is (and probably will be) no fix for this - mainly because there is no canonical action to take here; if the elements in the figure are too large, they are too large. (However in future versions, a warning will be emitted in such cases, see #10915)

    You need to decide for yourself what outcome you wish to have. Either make sure the labels or title are short enough not to overlap the axes by using a smaller fontsize or wrapping the text. Or, if you need those long labels, call tight layout before setting the long text to labels.

  7. QuLogic commented on Apr 7, 2018

    @QuLogic
    Member

    Closed by #10915.

  8. modified the milestones: , on Apr 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions