1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
import os
class MatplotlibBackendSetter(object):
"""
Import hook to make sure the proper Qt backend is set when importing
Matplotlib.
"""
enabled = True
def find_module(self, mod_name, pth=None):
if self.enabled and 'matplotlib' in mod_name:
self.enabled = False
set_mpl_backend()
def find_spec(self, name, import_path, target_module=None):
if self.enabled and name.startswith('matplotlib'):
self.enabled = False
set_mpl_backend()
def set_mpl_backend():
from matplotlib import rcParams, rcdefaults
# Standardize mpl setup
rcdefaults()
# Set default backend to Agg. The Qt and Jupyter glue applications don't
# use the default backend, so this is just to make sure that importing
# matplotlib doesn't cause errors related to the MacOSX or Qt backend.
rcParams['backend'] = 'Agg'
# Disable key bindings in matplotlib
for setting in list(rcParams.keys()):
if setting.startswith('keymap'):
rcParams[setting] = ''
# Set the MPLBACKEND variable explicitly, because ipykernel uses the lack of
# MPLBACKEND variable to indicate that it should use its own backend, and
# this in turn causes some rcParams to be changed, causing test failures
# etc.
os.environ['MPLBACKEND'] = 'Agg'
# Explicitly switch backend
from matplotlib.pyplot import switch_backend
switch_backend('agg')
|