Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
NelleV opened this issue Feb 10, 2017 · 5 comments
Closed

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

NelleV opened this issue Feb 10, 2017 · 5 comments
Milestone

Comments

@NelleV
Copy link
Member

NelleV commented Feb 10, 2017

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

@tacaswell tacaswell changed the title tight_layout + lots of subplots + long ylabels reverts yaxis tight_layout + lots of subplots + long ylabels inverts yaxis Feb 10, 2017
@anntzer
Copy link
Contributor

anntzer commented Feb 10, 2017

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...)

@amyspark
Copy link

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

@QuLogic
Copy link
Member

QuLogic commented Mar 30, 2018

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

@ImportanceOfBeingErnest
Copy link
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.

@QuLogic
Copy link
Member

QuLogic commented Apr 7, 2018

Closed by #10915.

@QuLogic QuLogic closed this as completed Apr 7, 2018
@QuLogic QuLogic modified the milestones: needs sorting, v3.0 Apr 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants