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

import pyplot issue - #14263

Closed
terranova42 opened this issue May 19, 2019 · 3 comments · Fixed by #14279
Closed

import pyplot issue - #14263

terranova42 opened this issue May 19, 2019 · 3 comments · Fixed by #14279
Milestone

Comments

@terranova42
Copy link

root@MyPC:/usr/local/lib/python3.7/dist-packages# ls -lh|grep mat
drwxr-sr-x 13 root staff 4,0K mai 19 20:01 matplotlib
drwxr-sr-x 2 root staff 4,0K mai 19 20:01 matplotlib-3.1.0+832.gc4382b0b6-py3.7.egg-info
drwxr-sr-x 6 root staff 4,0K mai 19 20:04 matplotlib-3.1.0+832.gc4382b0b6-py3.7-linux-x86_64.egg
-rw-r--r-- 1 root staff 569 mai 19 20:01 matplotlib-3.1.0+832.gc4382b0b6-py3.7-nspkg.pth
root@MyPC:/usr/local/lib/python3.7/dist-packages# python3
Python 3.7.1 (default, Oct 22 2018, 11:21:55)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

from matplotlib import pyplot
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.7/dist-packages/matplotlib/pyplot.py", line 2310, in
switch_backend(rcParams["backend"])
File "/usr/local/lib/python3.7/dist-packages/matplotlib/init.py", line 810, in getitem
plt.switch_backend(rcsetup._auto_backend_sentinel)
File "/usr/local/lib/python3.7/dist-packages/matplotlib/pyplot.py", line 212, in switch_backend
newbackend[9:] if newbackend.startswith("module://")
AttributeError: 'object' object has no attribute 'startswith'

@timhoffm
Copy link
Member

Seems like no backend could be set and we fell through all candidates in switch_backend.

This has two aspects:

  1. @terranova42 please try
    import matplotlib
    matplotlib.use('qt5agg')
    
    or some of the other backends listed in https://matplotlib.org/api/matplotlib_configuration_api.html?highlight=matplotlib%20use#matplotlib.use to see if you can manually set a backend.
  2. @ devs: We should catch that case for candiate in ... else [hande no backend could be loaded] - pyplot l.201ff.

@anntzer
Copy link
Contributor

anntzer commented May 19, 2019

switch_backend("agg") (the last case) should always succeed, unless something is seriously wrong with the install. So I guess the patch could be

diff --git i/lib/matplotlib/pyplot.py w/lib/matplotlib/pyplot.py
index 4f266917e..dda130995 100644
--- i/lib/matplotlib/pyplot.py
+++ w/lib/matplotlib/pyplot.py
@@ -198,8 +198,8 @@ def switch_backend(newbackend):
         # Don't try to fallback on the cairo-based backends as they each have
         # an additional dependency (pycairo) over the agg-based backend, and
         # are of worse quality.
-        for candidate in ["macosx", "qt5agg", "qt4agg", "gtk3agg", "tkagg",
-                          "wxagg", "agg"]:
+        for candidate in [
+                "macosx", "qt5agg", "qt4agg", "gtk3agg", "tkagg", "wxagg"]:
             try:
                 switch_backend(candidate)
             except ImportError:
@@ -207,6 +207,8 @@ def switch_backend(newbackend):
             else:
                 rcParamsOrig['backend'] = candidate
                 return
+        else:
+            switch_backend("agg")  # This should always succeed.
+            rcParamsOrig["backend"] = "agg"
+            return
 
     backend_name = (
         newbackend[9:] if newbackend.startswith("module://")

so that if switch_backend("agg") fails, one gets a proper exception out.

@terranova42
Copy link
Author

Hello,
the problem seemed to come from a broken dependency. I read this documentation:
https://gist.github.com/CMCDragonkai/4e9464d9f32f5893d837f3de2c43daa4
but at first I could not import default backend 'agg' or any other.
Fixed my install by upgrading pillow:

root@MyPC:/usr/lib/python3/dist-packages# python3
Python 3.7.1 (default, Oct 22 2018, 11:21:55)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

from PIL import Image
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 60, in
from . import _imaging as core
ImportError: cannot import name '_imaging' from 'PIL' (/usr/lib/python3/dist-packages/PIL/init.py)
exit()
root@MyPC:/usr/lib/python3/dist-packages# python3 -m pip install --upgrade pillow
Collecting pillow
Downloading https://files.pythonhosted.org/packages/c1/e6/ce127fa0ac17775bc7887c432ffe945c49ae141f01b477b7cd5e63b16bb5/
|████████████████████████████████| 2.0MB 16.3MB/s
Installing collected packages: pillow
Found existing installation: Pillow 5.1.0
Uninstalling Pillow-5.1.0:
Successfully uninstalled Pillow-5.1.0
Successfully installed pillow-6.0.0
root@MyPC:/usr/lib/python3/dist-packages# python3
Python 3.7.1 (default, Oct 22 2018, 11:21:55)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
from PIL import Image
exit()

pillow upgrade seems to be the root cause (?).
then I had to upgrade numpy from 1.13.3 to 1.16.3
then installed scipy 1.3
then I was able to import any backend.

import matplotlib.rcsetup as rcsetup
print(rcsetup.all_backends)
['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']
mpl.use('agg')
import matplotlib.pyplot as plt
plt.plot(range(20), range(20))
[<matplotlib.lines.Line2D object at 0x7ffa22c27240>]
plt.show()
main:1: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
mpl.use('TkAgg')
plt.show()
plt.plot(range(20), range(20))
[<matplotlib.lines.Line2D object at 0x7ffa1f69f7f0>]
plt.show()
print(rcsetup.interactive_bk)
['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo']

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