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

Skip to content

Commit 1f94c51

Browse files
committed
Use _repr_png_ with other suggested PR changes.
1 parent ff1384c commit 1f94c51

File tree

2 files changed

+17
-41
lines changed

2 files changed

+17
-41
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,44 +1701,24 @@ def __init__(self, figure):
17011701
self.toolbar = None # NavigationToolbar2 will set me
17021702
self._is_idle_drawing = False
17031703

1704-
def _repr_html_(self):
1705-
# Defer to IPython to handle html output if possible
1704+
def _repr_png_(self):
1705+
# Defer to IPython to handle output if possible.
17061706
if 'IPython' in sys.modules:
1707-
import IPython
1708-
ip = IPython.get_ipython()
1709-
# Check whether %matplotlib was run. Is there a better way?
1710-
ib_list = [c for c in ip.configurables
1711-
if 'InlineBackend' in type(c).__name__]
1712-
if ib_list:
1707+
from IPython.core.pylabtools import configure_inline_support
1708+
# Check whether %matplotlib was run.
1709+
if hasattr(configure_inline_support, 'current_backend'):
17131710
return
1714-
1715-
fmt = self.get_default_filetype()
1716-
1717-
kw = {
1718-
"format":fmt,
1719-
"facecolor":self.figure.get_facecolor(),
1720-
"edgecolor":self.figure.get_edgecolor(),
1721-
"dpi":self.figure.dpi,
1722-
"bbox_inches":self.figure.bbox_inches
1723-
}
17241711

1725-
bytes_io = io.BytesIO()
1726-
self.print_figure(bytes_io, **kw)
1727-
raw_bytes = bytes_io.getvalue()
1728-
1729-
from base64 import b64encode
1730-
data = b64encode(raw_bytes).decode()
1731-
1732-
if fmt == 'svg':
1733-
return raw_bytes.decode()
1734-
elif fmt == 'png':
1735-
return f'<img src="data:image/png;base64, {data}" />'
1736-
elif fmt == 'pdf':
1737-
w, h = self.figure.get_size_inches()
1738-
w, h = w * self.figure.dpi, h * self.figure.dpi
1739-
return f'<embed width="{w}" height="{h}" src="data:application/pdf;base64, {data}">'
1740-
elif fmt == 'jpg':
1741-
return f'<img src="data:image/jpeg;base64, {data}" />'
1712+
png_bytes = io.BytesIO()
1713+
self.print_figure(
1714+
png_bytes,
1715+
format='png',
1716+
facecolor=self.figure.get_facecolor(),
1717+
edgecolor=self.figure.get_edgecolor(),
1718+
dpi=self.figure.dpi,
1719+
bbox_inches=self.figure.bbox_inches
1720+
)
1721+
return png_bytes.getvalue()
17421722

17431723
@classmethod
17441724
@functools.lru_cache()

lib/matplotlib/figure.py

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

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

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

0 commit comments

Comments
 (0)