@@ -1596,71 +1596,43 @@ def __init__(self, figure):
15961596 self ._is_idle_drawing = False
15971597
15981598 def _repr_html_ (self ):
1599- if not self .figure .axes and not self .figure .lines :
1600- return
1601-
1602- dpi = self .figure .dpi
1603- is_retina = False
1604-
1605- import IPython
1606- ip = IPython .get_ipython ()
1607- ib_list = [c for c in ip .configurables
1608- if 'InlineBackend' in type (c ).__name__ ]
1609-
1610- # having an 'inline' backend doesn't mean '%matplotlib inline' has been run
1611- # Running %matplotlib inline runs pylabtools.configure_inline_support
1612- # which appends the InlineBackend to the list of configurables
1613- if get_backend () == 'module://ipykernel.pylab.backend_inline' and ib_list :
1614- ib = ib_list [0 ]
1615- bbox_inches = ib .print_figure_kwargs ['bbox_inches' ]
1616- fmt = next (iter (ib .figure_formats ))
1617- if fmt == 'retina' :
1618- is_retina = True
1619- dpi = dpi * 2
1620- fmt = self .get_default_filetype ()
1621- else :
1622- bbox_inches = 'tight' # how to let user choose self.figure.bbox_inches?
1623- fmt = self .get_default_filetype ()
1624-
1625- if fmt not in {'png' , 'svg' , 'jpg' , 'pdf' }:
1626- fmt = 'png'
1599+ # Defer to IPython to handle html output if possible
1600+ if 'IPython' in sys .modules :
1601+ import IPython
1602+ ip = IPython .get_ipython ()
1603+ # Check whether %matplotlib was run. Is there a better way?
1604+ ib_list = [c for c in ip .configurables
1605+ if 'InlineBackend' in type (c ).__name__ ]
1606+ if ib_list :
1607+ return
1608+
1609+ fmt = self .get_default_filetype ()
16271610
16281611 kw = {
16291612 "format" :fmt ,
16301613 "facecolor" :self .figure .get_facecolor (),
16311614 "edgecolor" :self .figure .get_edgecolor (),
1632- "dpi" :dpi ,
1633- "bbox_inches" :bbox_inches ,
1615+ "dpi" :self . figure . dpi ,
1616+ "bbox_inches" :self . figure . bbox_inches
16341617 }
16351618
16361619 bytes_io = io .BytesIO ()
16371620 self .print_figure (bytes_io , ** kw )
16381621 raw_bytes = bytes_io .getvalue ()
16391622
1640- from IPython .core .display import _pngxy , _jpegxy
1641-
1623+ from base64 import b64encode
1624+ data = b64encode (raw_bytes ).decode ()
1625+
16421626 if fmt == 'svg' :
16431627 return raw_bytes .decode ()
16441628 elif fmt == 'png' :
1645- w , h = _pngxy (raw_bytes )
1646- elif fmt == 'jpg' :
1647- w , h = _jpegxy (raw_bytes )
1629+ return f'<img src="data:image/png;base64, { data } " />'
16481630 elif fmt == 'pdf' :
16491631 w , h = self .figure .get_size_inches ()
1650- w , h = w * dpi , h * dpi
1651-
1652- if is_retina :
1653- w , h = w // 2 , h // 2
1654-
1655- from base64 import b64encode
1656- data = b64encode (raw_bytes ).decode ()
1657-
1658- if fmt == 'png' :
1659- return f'<img width="{ w } " height="{ h } " src="data:image/png;base64, { data } " />'
1660- elif fmt == 'pdf' :
1632+ w , h = w * self .figure .dpi , h * self .figure .dpi
16611633 return f'<embed width="{ w } " height="{ h } " src="data:application/pdf;base64, { data } ">'
16621634 elif fmt == 'jpg' :
1663- return f'<img width=" { w } " height=" { h } " src="data:image/jpeg;base64, { data } " />'
1635+ return f'<img src="data:image/jpeg;base64, { data } " />'
16641636
16651637 @classmethod
16661638 @functools .lru_cache ()
0 commit comments