Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions IPython/core/pylabtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs):
}
# **kwargs get higher priority
kw.update(kwargs)

bytes_io = BytesIO()
if fig.canvas is None:
from matplotlib.backend_bases import FigureCanvasBase
FigureCanvasBase(fig)

fig.canvas.print_figure(bytes_io, **kw)
data = bytes_io.getvalue()
if fmt == 'svg':
data = data.decode('utf-8')
return data

def retina_figure(fig, **kwargs):
"""format a figure as a pixel-doubled (retina) PNG"""
pngdata = print_figure(fig, fmt='retina', **kwargs)
Expand Down
6 changes: 6 additions & 0 deletions IPython/core/tests/test_pylabtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,9 @@ def test_qt_gtk(self):
def test_no_gui_backends():
for k in ['agg', 'svg', 'pdf', 'ps']:
assert k not in pt.backend2gui


def test_figure_no_canvas():
fig = Figure()
fig.canvas = None
pt.print_figure(fig)