You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tight_layout relies on get_renderer, which is not available on all backends #1852
With the MacOSX backend and possibly other backends, tight_layout falls back to the Agg backend. For example, demo_tight_layout.py does the following:
$ python -i examples/pylab_examples/demo_tight_layout.py
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.6-intel.egg/matplotlib/tight_layout.py:225: UserWarning: tight_layout : falling back to Agg renderer
warnings.warn("tight_layout : falling back to Agg renderer")
This is the relevant code in tight_layout.py:
def get_renderer(fig):
if fig._cachedRenderer:
renderer = fig._cachedRenderer
else:
canvas = fig.canvas
if canvas and hasattr(canvas, "get_renderer"):
renderer = canvas.get_renderer()
else:
# not sure if this can happen
warnings.warn("tight_layout : falling back to Agg renderer")
from matplotlib.backends.backend_agg import FigureCanvasAgg
canvas = FigureCanvasAgg(fig)
renderer = canvas.get_renderer()
return renderer
Neither backend_template.py nor backend_bases.py implement a get_renderer method, nor do the MacOSX and gtkcairo backend (and possibly other backends).
Even if I were to add a get_renderer method to the canvas in the MacOSX backend, this still fails. The problem is that this code tries to grab a renderer and use it outside of the event loop (i.e., outside of the figure.draw method). Outside of the event loop, the graphics context is not defined, and the code fails as follows:
$ python -i examples/pylab_examples/demo_tight_layout.py
Traceback (most recent call last):
File "examples/pylab_examples/demo_tight_layout.py", line 15, in
plt.tight_layout()
...
RuntimeError: CGContextRef is NULL
I would suggest to restructure the code in tight_layout.py to avoid using the renderer directly.
I noticed this too. It also happens with the PDF backend:
$ python
Python 2.7.3 (default, Nov 29 2012, 11:01:29)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license"for more information.
>>> import matplotlib
>>> matplotlib.use('pdf')
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(1, 1, 1)
>>> fig.tight_layout()
/Users/damon/python/lib/matplotlib-1.3.x-py2.7-macosx-10.8-x86_64.egg/matplotlib/tight_layout.py:225: UserWarning: tight_layout : falling back to Agg renderer
warnings.warn("tight_layout : falling back to Agg renderer")
We could move from using the renderer directly to updating the fig.draw() method so it returns the calculations needed by fig.tight_layout() from the renderer; utilising the event loop correctly.
I agree with @dmcdougall: I think that's what needs to be done. It has the added benefit of making tight_layout less sensitive to other backend differences.
@mdehoon , @dmcdougall : Just to clarify, tight_layout still works without error (only warning)? Or does it raise an exception?
The only reason that we need a renderer instance is to calculate the size and location of texts. @mdehoon, is this something impossible outside the event loop?
I am not quite clear about the suggestion by @dmcdougall. Does it mean that we call fig.draw inside tight_layout? I am not sure if that is a good idea.
tight_layout still works, but it works by falling back to the Agg renderer, which should not be necessary.
To calculate the size of texts, I presume you will need to call renderer.get_text_width_height_descent (either directly or indirectly). But renderer.get_text_width_height_descent needs a graphics context to be able to do its job, and it can only get a (properly initialized) graphics context from inside the callback in the event loop. This callback calls fig.draw, so I think @dmcdougall 's suggestion is to reorganize the tight_layout code such that the calculations are done as part of fig.draw.
We have Figure.set_tight_layout that makes Figure.draw to call tight_layout.
As far as I can see, tight_layout needs to be a one-time action that reflects the figure layout at the time when this function is called. And I don't see any easy way to achieve @dmcdougall 's suggestion given this behavior. But I will be happy to be proven wrong.
As far as I can see, we need to access renderer at the time when tight_layout is called. I don't see how we can avoid this. But again, I will be happy to be proven wrong.
@leejjoon The method Figure.set_tight_layout seems to be what we're trying to achieve, I think. The PDF backend now works this way:
In [1]: importmatplotlibIn [2]: matplotlib.use('pdf')
In [3]: importmatplotlib.pyplotaspltIn [4]: fig=plt.figure()
In [5]: ax=fig.add_subplot(1,1,1)
In [6]: fig.set_tight_layout(True)
In [7]: fig.savefig('asd.pdf') # No warning now
@mdehoon I'm on a linux box at the moment, would you be able to try this on OS X?
If this works on Mac, could we add tight_layout kwarg to fig.savefig that, when set to True, Figure.set_tight_layout gets called?
@dmcdougall : fig.set_tight_layout seems to work fine with the MacOSX backend.
I noted though that with your example, fig.add_subplot does nothing in interactive mode, even if I skip the fig.set_tight_layout command.
In non-interactive mode the script works as expected.
Also if I replace fig.add_subplot by plt.subplot, then the set_tight_layout works fine, both in interactive and in non-interactive mode.
So it looks like set_tight_layout is working fine. I have no idea why fig.add_subplot does nothing while plt.subplot works fine, but anyway this is independent of set_tight_layout.
@dmcdougall It turned out that the difference between fig.add_subplot and plt.subplot is due to the fact that the latter includes a call to draw_if_interactive while the former doesn't. If I add plt.draw_if_interactive() to your example, fig.set_tight_layout works fine with the MacOSX backend both in interactive and in non-interactive mode.
@leejjoon@dmcdougall@mdehoon : Thank you to all of you... the fig.set_tight_layout(True) tip has been very useful to avoid the warning, the backend issue and to get even a better appearance than plt.tight_layout().
not sure if this is helpful: for me, calling fig.tight_layout() resulted in RuntimeError: CGContextRef is NULL errors, while fig.set_tight_layout(True) worked.
OS X, osx backend, python 2.7.5, mpl 1.3.1
See issue #2654 for more information; essentially, these two are the same problem. Redesigning the tight_layout code, as proposed in issue #2654 , should solve the problem.
Activity
dmcdougall commentedon Mar 24, 2013
I noticed this too. It also happens with the PDF backend:
We could move from using the renderer directly to updating the
fig.draw()method so it returns the calculations needed byfig.tight_layout()from the renderer; utilising the event loop correctly.mdboom commentedon Mar 25, 2013
I agree with @dmcdougall: I think that's what needs to be done. It has the added benefit of making
tight_layoutless sensitive to other backend differences.leejjoon commentedon Apr 1, 2013
@mdehoon , @dmcdougall : Just to clarify, tight_layout still works without error (only warning)? Or does it raise an exception?
The only reason that we need a renderer instance is to calculate the size and location of texts. @mdehoon, is this something impossible outside the event loop?
I am not quite clear about the suggestion by @dmcdougall. Does it mean that we call
fig.drawinsidetight_layout? I am not sure if that is a good idea.mdehoon commentedon Apr 1, 2013
tight_layout still works, but it works by falling back to the Agg renderer, which should not be necessary.
To calculate the size of texts, I presume you will need to call renderer.get_text_width_height_descent (either directly or indirectly). But renderer.get_text_width_height_descent needs a graphics context to be able to do its job, and it can only get a (properly initialized) graphics context from inside the callback in the event loop. This callback calls fig.draw, so I think @dmcdougall 's suggestion is to reorganize the tight_layout code such that the calculations are done as part of fig.draw.
leejjoon commentedon Apr 3, 2013
We have
Figure.set_tight_layoutthat makesFigure.drawto calltight_layout.As far as I can see,
tight_layoutneeds to be a one-time action that reflects the figure layout at the time when this function is called. And I don't see any easy way to achieve @dmcdougall 's suggestion given this behavior. But I will be happy to be proven wrong.As far as I can see, we need to access renderer at the time when
tight_layoutis called. I don't see how we can avoid this. But again, I will be happy to be proven wrong.dmcdougall commentedon Apr 3, 2013
@leejjoon The method
Figure.set_tight_layoutseems to be what we're trying to achieve, I think. The PDF backend now works this way:@mdehoon I'm on a linux box at the moment, would you be able to try this on OS X?
If this works on Mac, could we add
tight_layoutkwarg tofig.savefigthat, when set toTrue,Figure.set_tight_layoutgets called?mdehoon commentedon Apr 4, 2013
@dmcdougall : fig.set_tight_layout seems to work fine with the MacOSX backend.
I noted though that with your example, fig.add_subplot does nothing in interactive mode, even if I skip the fig.set_tight_layout command.
In non-interactive mode the script works as expected.
Also if I replace fig.add_subplot by plt.subplot, then the set_tight_layout works fine, both in interactive and in non-interactive mode.
So it looks like set_tight_layout is working fine. I have no idea why fig.add_subplot does nothing while plt.subplot works fine, but anyway this is independent of set_tight_layout.
mdehoon commentedon Apr 4, 2013
@dmcdougall It turned out that the difference between fig.add_subplot and plt.subplot is due to the fact that the latter includes a call to draw_if_interactive while the former doesn't. If I add plt.draw_if_interactive() to your example, fig.set_tight_layout works fine with the MacOSX backend both in interactive and in non-interactive mode.
dmcdougall commentedon Apr 4, 2013
@mdehoon Yeah, sorry. I rarely use the interactive stuff. My go-to is the PDF or Agg backend.
khyox commentedon Aug 7, 2013
@leejjoon @dmcdougall @mdehoon : Thank you to all of you... the
fig.set_tight_layout(True)tip has been very useful to avoid the warning, the backend issue and to get even a better appearance thanplt.tight_layout().nilsbecker commentedon Dec 11, 2013
not sure if this is helpful: for me, calling
fig.tight_layout()resulted inRuntimeError: CGContextRef is NULLerrors, whilefig.set_tight_layout(True)worked.OS X, osx backend, python 2.7.5, mpl 1.3.1
mdehoon commentedon Dec 26, 2013
See issue #2654 for more information; essentially, these two are the same problem. Redesigning the tight_layout code, as proposed in issue #2654 , should solve the problem.
Change tight_layout commands to avoid errors on Mac OSX
2 remaining items
Supprss a backend warning caused in tight_layout on OSX
aflansburg commentedon Mar 6, 2015
@c-benko Error is present on my Mac OSX 10.10.1 as well. Python 2.7.9, matplotlib 1.4.0 backend "MacOSX"
Using
and calling
fix broken plot on mac osx #1852
fix broken plot on mac osx #1852 (#283)
diego898 commentedon Jan 13, 2017
I am seeing this warning while running anything using pycharm and anaconda with python 3.5.2 and matplotlib 1.5.3.
I dont see this if I run through the jupyter notebook.
janssen commentedon Jan 21, 2017
I'm seeing this with 2.0.0rc1 and the Kivy backend.
bayesfactor commentedon May 16, 2018
I'm having this issue with SVG backend on Windows + python 3.5.5 + matplotlib 2.1.1
remove tight_layout() from figure saving code
smithsp commentedon Mar 22, 2019
Bump for PDF backend.
anntzer commentedon Feb 24, 2022
Closed by #15221, I think.