34
34
35
35
from contextlib import contextmanager
36
36
from enum import IntEnum
37
+ import functools
37
38
import importlib
38
39
import io
39
40
import logging
44
45
45
46
import numpy as np
46
47
48
+ import matplotlib as mpl
47
49
from matplotlib import (
48
50
backend_tools as tools , cbook , colors , textpath , tight_bbox , transforms ,
49
51
widgets , get_backend , is_interactive , rcParams )
@@ -1563,6 +1565,7 @@ class FigureCanvasBase(object):
1563
1565
'Tagged Image File Format' )
1564
1566
1565
1567
def __init__ (self , figure ):
1568
+ self ._fix_ipython_backend2gui ()
1566
1569
self ._is_idle_drawing = True
1567
1570
self ._is_saving = False
1568
1571
figure .set_canvas (self )
@@ -1579,6 +1582,40 @@ def __init__(self, figure):
1579
1582
self .toolbar = None # NavigationToolbar2 will set me
1580
1583
self ._is_idle_drawing = False
1581
1584
1585
+ @classmethod
1586
+ @functools .lru_cache ()
1587
+ def _fix_ipython_backend2gui (cls ):
1588
+ # Fix hard-coded module -> toolkit mapping in IPython (used for
1589
+ # `ipython --auto`). This cannot be done at import time due to
1590
+ # ordering issues, so we do it when creating a canvas, and should only
1591
+ # be done once per class (hence the `lru_cache(1)`).
1592
+ if "IPython" not in sys .modules :
1593
+ return
1594
+ import IPython
1595
+ ip = IPython .get_ipython ()
1596
+ if not ip :
1597
+ return
1598
+ from IPython .core import pylabtools as pt
1599
+ if (not hasattr (pt , "backend2gui" )
1600
+ or not hasattr (ip , "enable_matplotlib" )):
1601
+ # In case we ever move the patch to IPython and remove these APIs,
1602
+ # don't break on our side.
1603
+ return
1604
+ backend_mod = sys .modules [cls .__module__ ]
1605
+ rif = getattr (backend_mod , "required_interactive_framework" , None )
1606
+ backend2gui_rif = {"qt5" : "qt" , "qt4" : "qt" , "gtk3" : "gtk3" ,
1607
+ "wx" : "wx" , "macosx" : "osx" }.get (rif )
1608
+ if backend2gui_rif :
1609
+ pt .backend2gui [get_backend ()] = backend2gui_rif
1610
+ # Work around pylabtools.find_gui_and_backend always reading from
1611
+ # rcParamsOrig.
1612
+ orig_origbackend = mpl .rcParamsOrig ["backend" ]
1613
+ try :
1614
+ mpl .rcParamsOrig ["backend" ] = mpl .rcParams ["backend" ]
1615
+ ip .enable_matplotlib ()
1616
+ finally :
1617
+ mpl .rcParamsOrig ["backend" ] = orig_origbackend
1618
+
1582
1619
@contextmanager
1583
1620
def _idle_draw_cntx (self ):
1584
1621
self ._is_idle_drawing = True
0 commit comments