2

I want to use matplotlib's tight_layout function on a figure with only one plot in it, but it is giving me an AssertionError. This is my code: (Note: updated w.r.t. original post)

def plot_histogram_00():    
    fig, _ = plt.subplots(figsize=(9, 5))        
    fig.tight_layout()
    fig.close()

When I run this in Eclipse on my Windows 7 computer (Python 2.7, matplotlib 1.4), it runs fine, and I get a nice output. However, when I run exactly the same code (run from a Dropbox folder) on my Mac computer (also Python 2.7, matplotlib 1.4), I receive the following Error message:

Traceback (most recent call last):
  File "/Users/david/Dropbox/Sandbox/Histogram.py", line 139, in <module>
    plot_histogram_00();
  File "/Users/david/Dropbox/Sandbox/Histogram.py", line 38, in plot_histogram_00
    fig.tight_layout()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 1654, in tight_layout
    rect=rect)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py", line 352, in get_tight_layout_figure
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py", line 131, in auto_adjust_subplotpars
    fig.transFigure.inverted())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/matplotlib/transforms.py", line 1057, in __init__
    assert isinstance(transform, Transform)
AssertionError

Is someone able to point me in a direction where I shall start looking for the cause of this error?

UPDATE: My original code was this:

import matplotlib
matplotlib.use('TkAgg') 
import matplotlib.pyplot as plt
import numpy as np

def plot_histogram_00():
    xlabels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
               'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    np.random.seed(1)
    values_A = np.random.choice(np.arange(len(xlabels)), 
                                size=200, replace=True).tolist()
    values_B = np.random.choice(np.arange(len(xlabels)), 
                                size=200, replace=True).tolist()

    fig, ax = plt.subplots(figsize=(9, 5))
    _, bins, patches = plt.hist([values_A, values_B], normed=1,
                                bins=len(xlabels),
                                color=['#3782CC', '#AFD5FA'],
                                label=['A', 'B'])

    fig.tight_layout()
    plt.savefig('my_plot_00.png')
4
  • Are all of these lines needed to trigger the error?
    – Veedrac
    Oct 7, 2014 at 14:01
  • odd.... Also make sure to clean up your figures (pyplot keeps a global list, fig.close() after the save should be enough)
    – tacaswell
    Oct 7, 2014 at 16:14
  • Indeed not all lines are needed, the plot itself for instance is not. I updated the original post, including the fig.close() statement. I still observe this behavior (working in Windows, not working in Mac). Oct 8, 2014 at 8:15
  • I am unable to reproduce your error with python 2.7.8, matplotlib 1.4.0, on my mac. Maybe you need to reinstall matplotlib on the mac? Oct 9, 2014 at 14:49

1 Answer 1

2

I had the same issue for mac using a matplotlib install managed by brew-python. Fixed it by completely removing it in brew (brew uninstall --force matplotlib) and then remove the remaining source folders in /usr/local/lib/python2.7/site-packages. Reinstall from pip: pip install matplotlib.

Main reason I used brew-python before was an issue with matplotlib not having a standard setup.py script. As this seems fixed in the latest installation of matplotlib I suppose I can completely rely on pip now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.