tight_layout() causes RuntimeError on OSX

Hey guys, I’m trying to use tight_layout() to fix up some subplot grossness, but whenever I call it, I get:

Traceback (most recent call last):

File “test.py”, line 16, in

plt.tight_layout()

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py”, line 1056, in tight_layout

fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py”, line 1415, in tight_layout

pad=pad, h_pad=h_pad, w_pad=w_pad)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py”, line 117, in auto_adjust_subplotpars

tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots])

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py”, line 8396, in get_tightbbox

bb.append(self.title.get_window_extent(renderer))

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/text.py”, line 743, in get_window_extent

bbox, info = self._get_layout(self._renderer)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/text.py”, line 296, in _get_layout

ismath=False)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py”, line 164, in get_text_width_height_descent

width, height, descent = self.gc.get_text_width_height_descent(unicode(s), family, size, weight, style)

RuntimeError: CGContextRef is NULL

I’ve googled around, but no answers have been found. Is this a known issue, and if it’s not, how can I go about fixing it?

Thanks,

-E

When I run the macosx backend (which appears to be what you’re using), I get the following output:

tight_layout : falling back to Agg renderer

and the figure appears to be rendered with TkAgg . So it appears that it’s a known issue on the macosx backend. And, apparently, the suggested work-around is to use an Agg backend.

-Tony

···

On Tue, May 1, 2012 at 3:04 PM, Elliot Saba <staticfloat@…287…> wrote:

Hey guys, I’m trying to use tight_layout() to fix up some subplot grossness, but whenever I call it, I get:

Traceback (most recent call last):

File “test.py”, line 16, in

plt.tight_layout()

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py”, line 1056, in tight_layout

fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py”, line 1415, in tight_layout

pad=pad, h_pad=h_pad, w_pad=w_pad)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py”, line 117, in auto_adjust_subplotpars

tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots])

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py”, line 8396, in get_tightbbox

bb.append(self.title.get_window_extent(renderer))

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/text.py”, line 743, in get_window_extent

bbox, info = self._get_layout(self._renderer)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/text.py”, line 296, in _get_layout

ismath=False)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py”, line 164, in get_text_width_height_descent

width, height, descent = self.gc.get_text_width_height_descent(unicode(s), family, size, weight, style)

RuntimeError: CGContextRef is NULL

I’ve googled around, but no answers have been found. Is this a known issue, and if it’s not, how can I go about fixing it?

Thanks,

-E

That does indeed work. Thank you.

-E

···

On Tue, May 1, 2012 at 6:45 PM, Tony Yu <tsyu80@…287…> wrote:

On Tue, May 1, 2012 at 3:04 PM, Elliot Saba <staticfloat@…287…> wrote:

Hey guys, I’m trying to use tight_layout() to fix up some subplot grossness, but whenever I call it, I get:

Traceback (most recent call last):

File “test.py”, line 16, in

plt.tight_layout()

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py”, line 1056, in tight_layout

fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py”, line 1415, in tight_layout

pad=pad, h_pad=h_pad, w_pad=w_pad)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py”, line 117, in auto_adjust_subplotpars

tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots])

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py”, line 8396, in get_tightbbox

bb.append(self.title.get_window_extent(renderer))

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/text.py”, line 743, in get_window_extent

bbox, info = self._get_layout(self._renderer)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/text.py”, line 296, in _get_layout

ismath=False)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py”, line 164, in get_text_width_height_descent

width, height, descent = self.gc.get_text_width_height_descent(unicode(s), family, size, weight, style)

RuntimeError: CGContextRef is NULL

I’ve googled around, but no answers have been found. Is this a known issue, and if it’s not, how can I go about fixing it?

Thanks,

-E

When I run the macosx backend (which appears to be what you’re using), I get the following output:

tight_layout : falling back to Agg renderer

and the figure appears to be rendered with TkAgg . So it appears that it’s a known issue on the macosx backend. And, apparently, the suggested work-around is to use an Agg backend.

-Tony

This kind of error typically occurs when the graphics context is being accessed outside of the event loop. This may work for other backends, but on Mac OS X in such a case the graphics context is not defined. The solution may involve reorganizing tight_layout such that the graphics context is not being accessed outside of the event loop.
-Michiel.

···

— On Tue, 5/1/12, Elliot Saba <staticfloat@…878…287…> wrote:

From: Elliot Saba <staticfloat@…287…>
Subject: [Matplotlib-users] tight_layout() causes RuntimeError on OSX
To: “Matplotlib Users” matplotlib-users@lists.sourceforge.net
Date: Tuesday, May 1, 2012, 3:04 PM

Hey guys, I’m trying to use tight_layout() to fix up some
subplot grossness, but whenever I call it, I get:

Traceback (most recent call last):

File “test.py”, line 16, in

plt.tight_layout()

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py”, line 1056, in tight_layout

fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py”, line 1415, in tight_layout

pad=pad, h_pad=h_pad, w_pad=w_pad)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py”, line 117, in auto_adjust_subplotpars

tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots])

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py”, line 8396, in get_tightbbox

bb.append(self.title.get_window_extent(renderer))

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/text.py”, line 743, in get_window_extent

bbox, info = self._get_layout(self._renderer)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/text.py”, line 296, in _get_layout

ismath=False)

File “/Users//.homebrew/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py”, line 164, in get_text_width_height_descent

width, height, descent = self.gc.get_text_width_height_descent(unicode(s), family, size, weight, style)

RuntimeError: CGContextRef is NULL

I’ve googled around, but no answers have been found. Is this a known issue, and if it’s not, how can I go about fixing it?

Thanks,

-E

-----Inline Attachment Follows-----


Live Security Virtual Conference
Exclusive live event will cover all the ways today’s security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

-----Inline Attachment Follows-----


Matplotlib-users mailing list
Matplotlib-users@…1739…ge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users