0

I recently switched to new macbook pro(from 2015 to 2017).

From

macOS Sierra 10.12.6
conda 4.3.30
Jupyter 4.1.0
Python 2.7.11 | Annaconda custom(x86_64) [GCC 4.2.1 (Apple Inc. build 5577) on darwin

To

High Sierra 10.13.2
Jupyter 4.4.0
conda 4.3.30
Python 2.7.14 |Anaconda custom (64-bit)| (default, Dec  7 2017, 11:07:58) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin   

I moved all my data by restoring the new computer with fresh timemachine backup from old computer. Everything works well except matplotlib.

In Jupyter notebook,

If i run,

import matplotlib.pyplot as plt 
plt.plot(range(10))
plt.show()   

It works okay with plot showing inline on notebook.

But if I do %matplotlib

It goes to Using matplotlib backend: MacOSX

but following message is displayed in terminal,

[I 13:09:59.765 NotebookApp] Replaying 6 buffered messages
ERROR:tornado.application:Exception in callback <functools.partial object at 0x112208fc8>
Traceback (most recent call last):
  File "//anaconda/lib/python2.7/site-packages/tornado/ioloop.py", line 605, in _run_callback
    ret = callback()
  File "//anaconda/lib/python2.7/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "//anaconda/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 262, in enter_eventloop
    self.eventloop(self)
  File "//anaconda/lib/python2.7/site-packages/ipykernel/eventloops.py", line 278, in loop_cocoa
    show.mainloop()
AttributeError: 'function' object has no attribute 'mainloop'   

And then now, plot does not show up.

plt.plot(range(10))
plt.show()

This will now result in nothing. The cell executes but nothing happens.

5
  • You may try to edit your matplotlib rc file to set the backend to a different one, e.g. backend : TkAgg. Commented Jan 10, 2018 at 13:02
  • i saw this mentioned somewhere but I could not find matplotlibrc file Commented Jan 10, 2018 at 13:04
  • Before doing anything else, try if a different backend even changes anything. To this end try %matplotlib tk instead of %matplotlib without any extention. Commented Jan 10, 2018 at 13:28
  • hey it worked for simple plot like above but for a 3D plot, it does not display anything. :( the window opens but the figure i blank. Commented Jan 10, 2018 at 13:37
  • 1
    funny thing is if I use import matplotlib and `matplotlib.use('tkAgg'), the plot is plotted in new window but if i close that window, now the cell does not stop executing. I have to force stop the cell through jupyter Commented Jan 10, 2018 at 13:42

1 Answer 1

1

I'm on Windows 10 with Python 3.6 and the latest matplotlib package. This has worked for me:

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

fig, ax = plt.subplots(1,2)
plt.show(block=False)
for i in range(100):
    print(i)
    a = np.random.rand(10,10)
    ax[0].imshow(a)
    a = np.random.rand(10,10)
    ax[1].imshow(a)
    fig.canvas.draw()

A call to matplotlib.use('tkAgg') has to be before any use of matplot in the code. The key part is fig.canvas.draw()

The code above works for me, but when it's a real CPU heavy code, it may take up all power so that the plots are not updated until the loop is over. In this case adding this statement plt.pause(1e-2) right after draw() helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.