Skip to content

Using backend Template and plt.tight_layout raises UnboundLocalError #17730

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

Closed
jni opened this issue Jun 23, 2020 · 3 comments · Fixed by #17731
Closed

Using backend Template and plt.tight_layout raises UnboundLocalError #17730

jni opened this issue Jun 23, 2020 · 3 comments · Fixed by #17731
Milestone

Comments

@jni
Copy link
Contributor

jni commented Jun 23, 2020

Matplotlib 3.3.0rc1 appears to be breaking our documentation builds at scikit-image, see

https://travis-ci.org/github/scikit-image/scikit-image/jobs/701180133#L5665-L5691

I was able to create a minimal example that reproduces the error on my machine, see code below.

Bug report

Bug summary

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

fig, axes = plt.subplots()
axes.imshow(np.random.random((5, 5)), cmap=plt.cm.gray)
plt.tight_layout()

Actual outcome

Traceback (most recent call last):
  File "mpl_error.py", line 8, in <module>
    plt.tight_layout()
  File "/home/jni/miniconda3/envs/skimage-test/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py", line 450, in wrapper
    return func(*args, **kwargs)
  File "/home/jni/miniconda3/envs/skimage-test/lib/python3.8/site-packages/matplotlib/pyplot.py", line 1489, in tight_layout
    gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "/home/jni/miniconda3/envs/skimage-test/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py", line 410, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "/home/jni/miniconda3/envs/skimage-test/lib/python3.8/site-packages/matplotlib/figure.py", line 2604, in tight_layout
    renderer = get_renderer(self)
  File "/home/jni/miniconda3/envs/skimage-test/lib/python3.8/site-packages/matplotlib/tight_layout.py", line 176, in get_renderer
    return backend_bases._get_renderer(fig)
  File "/home/jni/miniconda3/envs/skimage-test/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1556, in _get_renderer
    return renderer
UnboundLocalError: local variable 'renderer' referenced before assignment

Expected outcome

No crash.

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 3.3.0rc1
  • Matplotlib backend (print(matplotlib.get_backend())): Template
  • Python version: 3.8.3
  • Jupyter version (if applicable): N/A
  • Other libraries: 1.19.0

Installation

conda create -n matplotlib-rc python=3.8
conda activate matplotlib-rc
pip install --pre matplotlib numpy
@jklymak
Copy link
Member

jklymak commented Jun 23, 2020

Thanks for checking the RC! Much appreciated.

Is there a reason you are using "Template" instead of "Agg"? I don't think we meant for template to be public (in the mists of time).

@tacaswell tacaswell added this to the v3.3.0 milestone Jun 23, 2020
@QuLogic
Copy link
Member

QuLogic commented Jun 23, 2020

Does the Template backend actually do anything? I would not expect it to actually produce any usable figures.

@jni
Copy link
Contributor Author

jni commented Jun 23, 2020

Thanks for checking the RC! Much appreciated.

Yeah I'm so happy we set up pre-release testing! 😊

Is there a reason you are using "Template" instead of "Agg"?

Dunno! The mists of time is a good way to describe the time when that part of the config was written. I can try changing it to Agg and see if that fixes our CI, but still, UnboundLocalErrors should not be possible. 🙂

I would not expect it to actually produce any usable figures.

It's not supposed to, this particular check in our CI just tests that the examples run without incident.

Also, if Template does approximately nothing, would that make the examples run faster?

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

Successfully merging a pull request may close this issue.

4 participants