简体   繁体   中英

%Matplotlib - AttributeError: 'NoneType' object has no attribute 'lower'

I'm running %matplotlib in IPython, but there is a AttributeError: 'NoneType' object has no attribute 'lower'

Python 3.7; MacOs Mojave 10.14.6 (18G84); conda matplotlib: 3.1.1 pypi_0 pypi

In [13]: import matplotlib                                                      

In [14]: import matplotlib.pyplot as plt                                        

In [15]: %matplotlib                                                            
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-a49a4afc47c5> in <module>
----> 1 get_ipython().run_line_magic('matplotlib', '')

//anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2311                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2312             with self.builtin_trap:
-> 2313                 result = fn(*args, **kwargs)
   2314             return result
   2315 

<//anaconda3/lib/python3.7/site-packages/decorator.py:decorator-gen-108> in matplotlib(self, line)

//anaconda3/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

//anaconda3/lib/python3.7/site-packages/IPython/core/magics/pylab.py in matplotlib(self, line)
     97             print("Available matplotlib backends: %s" % backends_list)
     98         else:
---> 99             gui, backend = self.shell.enable_matplotlib(args.gui.lower())
    100             self._show_matplotlib_backend(args.gui, backend)
    101 

AttributeError: 'NoneType' object has no attribute 'lower'

According to the docs %matplotlib may be supplied with a backend (including, for example, 'inline', 'notebook', 'gtk','qt4' etc.). If a backend option is not supplied it should use the default. It looks like this was broken in some version(s) of ipython (see here and here ). I've also had some trouble with %matplotlib commands with some versions of matplotlib .

So, try updating ipython and/or matplotlib if possible and see if that fixes things.

Alternatively, use %matplotlib --list to get available options, then pick one and try (for example):

%matplotlib qt

If you get into some fancy plotting stuff, sometimes one backend won't work as well as another for a particular case, so you can test run a few and use what works (or is available). Also, if you want to do things directly with figure windows (such as moving figures around on your screen) then those become backend specific. If you're in a jupyter notebook try %matplotlib inline or %matplotlib notebook .

Got here with a similar error message with Jupyter Lab. Was solved for me by placing the backend magic

%matplotlib inline

in the cell actually doing the plotting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM