diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py
index 18a750c9f46d..bf87c9c43796 100644
--- a/lib/matplotlib/backend_bases.py
+++ b/lib/matplotlib/backend_bases.py
@@ -1595,6 +1595,45 @@ def __init__(self, figure):
self.toolbar = None # NavigationToolbar2 will set me
self._is_idle_drawing = False
+ def _repr_html_(self):
+ # Defer to IPython to handle html output if possible
+ if 'IPython' in sys.modules:
+ import IPython
+ ip = IPython.get_ipython()
+ # Check whether %matplotlib was run. Is there a better way?
+ ib_list = [c for c in ip.configurables
+ if 'InlineBackend' in type(c).__name__]
+ if ib_list:
+ return
+
+ fmt = self.get_default_filetype()
+
+ kw = {
+ "format":fmt,
+ "facecolor":self.figure.get_facecolor(),
+ "edgecolor":self.figure.get_edgecolor(),
+ "dpi":self.figure.dpi,
+ "bbox_inches":self.figure.bbox_inches
+ }
+
+ bytes_io = io.BytesIO()
+ self.print_figure(bytes_io, **kw)
+ raw_bytes = bytes_io.getvalue()
+
+ from base64 import b64encode
+ data = b64encode(raw_bytes).decode()
+
+ if fmt == 'svg':
+ return raw_bytes.decode()
+ elif fmt == 'png':
+ return f'
'
+ elif fmt == 'pdf':
+ w, h = self.figure.get_size_inches()
+ w, h = w * self.figure.dpi, h * self.figure.dpi
+ return f'