Use Qt5Agg GUI backend for matplotlib on MacOS


If you use matplotlib to plot diagram on MacOS, you may get following error.

ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install and replace the use of ‘python’ with ‘pythonw’. See ‘Working with Matplotlib on OSX’ in the Matplotlib FAQ for more information.

Checkout the file matplotlibrc in ‘site-packages/matplotlib/mpl-data/‘ and we know we should use one of the following backends:

  • MacOSX
  • Qt5Agg
  • Gtk3Agg


If you use MacOSX as the backend, you should reinstall your python with the parameter --with-tcl-tk. I use pyenv & virtualenv to manage my python versions and I do not know how to deal with it. So i try the second option — Qt5Agg and it worked.

Install PyQt5

$ brew install pyqt
$ pip install PyQt5

Change Backend

Add the following code to the top of the file

import matplotlib


import matplotlib

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

speed = 0.03
fig, ax = plt.subplots()
x = np.arange(0, 2 * np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

ani = animation.FuncAnimation(
    func=lambda i: line.set_ydata(np.sin(x + i * speed)),
    frames=int(2 * np.pi / speed),
    init_func=lambda: line.set_ydata(np.sin(x)),

本文作者: Vincent0700
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!