Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46e810e

Browse files
committed
Use _repr_png_ with other suggested PR changes.
1 parent b09de9c commit 46e810e

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,44 +1595,24 @@ def __init__(self, figure):
15951595
self.toolbar = None # NavigationToolbar2 will set me
15961596
self._is_idle_drawing = False
15971597

1598-
def _repr_html_(self):
1599-
# Defer to IPython to handle html output if possible
1598+
def _repr_png_(self):
1599+
# Defer to IPython to handle output if possible.
16001600
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:
1601+
from IPython.core.pylabtools import configure_inline_support
1602+
# Check whether %matplotlib was run.
1603+
if hasattr(configure_inline_support, 'current_backend'):
16071604
return
1608-
1609-
fmt = self.get_default_filetype()
1610-
1611-
kw = {
1612-
"format":fmt,
1613-
"facecolor":self.figure.get_facecolor(),
1614-
"edgecolor":self.figure.get_edgecolor(),
1615-
"dpi":self.figure.dpi,
1616-
"bbox_inches":self.figure.bbox_inches
1617-
}
1618-
1619-
bytes_io = io.BytesIO()
1620-
self.print_figure(bytes_io, **kw)
1621-
raw_bytes = bytes_io.getvalue()
1622-
1623-
from base64 import b64encode
1624-
data = b64encode(raw_bytes).decode()
1625-
1626-
if fmt == 'svg':
1627-
return raw_bytes.decode()
1628-
elif fmt == 'png':
1629-
return f'<img src="data:image/png;base64, {data}" />'
1630-
elif fmt == 'pdf':
1631-
w, h = self.figure.get_size_inches()
1632-
w, h = w * self.figure.dpi, h * self.figure.dpi
1633-
return f'<embed width="{w}" height="{h}" src="data:application/pdf;base64, {data}">'
1634-
elif fmt == 'jpg':
1635-
return f'<img src="data:image/jpeg;base64, {data}" />'
1605+
1606+
png_bytes = io.BytesIO()
1607+
self.print_figure(
1608+
png_bytes,
1609+
format='png',
1610+
facecolor=self.figure.get_facecolor(),
1611+
edgecolor=self.figure.get_edgecolor(),
1612+
dpi=self.figure.dpi,
1613+
bbox_inches=self.figure.bbox_inches
1614+
)
1615+
return png_bytes.getvalue()
16361616

16371617
@classmethod
16381618
@functools.lru_cache()

lib/matplotlib/figure.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,8 @@ def __init__(self,
364364
# list of child gridspecs for this figure
365365
self._gridspecs = []
366366

367-
# TODO: I'd like to dynamically add the _repr_html_ method
368-
# to the figure in the right context, but then IPython doesn't
369-
# use it, for some reason.
370-
371-
def _repr_html_(self):
372-
return self.canvas._repr_html_()
367+
def _repr_png_(self):
368+
return self.canvas._repr_png_()
373369

374370
def show(self, warn=True):
375371
"""

0 commit comments

Comments
 (0)