Browse files

Add support for displaying maptlotlib axes directly.

Currently, to display an Axes instance one must grab its .figure
attribute manually.  Since there is no other reasonable representation
of an Axis than to show it, and so many matplotlib methods return
Axes, displaying them directly seems to be the most reasonable solution.
  • Loading branch information...
1 parent a1ccd55 commit bfa368e4332f7470949a225c9d283b83dcedda92 @fperez committed Dec 14, 2011
Showing with 16 additions and 1 deletion.
  1. +16 −1 IPython/core/
@@ -92,7 +92,7 @@ def print_figure(fig, fmt='png'):
"""Convert a figure to svg or png for inline display."""
# When there's an empty figure, we shouldn't return anything, otherwise we
# get big blank areas in the qt console.
- if not fig.axes:
+ if not fig.axes and not fig.lines:
fc = fig.get_facecolor()
@@ -109,6 +109,11 @@ def print_figure(fig, fmt='png'):
return data
+def print_axes(ax, fmt='png'):
+ """Print an Axes object via its associated figure"""
+ return print_figure(ax.figure, fmt)
# We need a little factory function here to create the closure where
# safe_execfile can live.
def mpl_runner(safe_execfile):
@@ -156,7 +161,9 @@ def select_figure_format(shell, fmt):
Using this method ensures only one figure format is active at a time.
+ from matplotlib.axes import Axes
from matplotlib.figure import Figure
+ from matplotlib.image import AxesImage
from IPython.zmq.pylab import backend_inline
svg_formatter = shell.display_formatter.formatters['image/svg+xml']
@@ -165,9 +172,17 @@ def select_figure_format(shell, fmt):
if fmt=='png':
svg_formatter.type_printers.pop(Figure, None)
png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png'))
+ svg_formatter.type_printers.pop(Axes, None)
+ png_formatter.for_type(Axes, lambda ax: print_axes(ax, 'png'))
+ svg_formatter.type_printers.pop(AxesImage, None)
+ png_formatter.for_type(AxesImage, lambda ax: print_axes(ax, 'png'))
elif fmt=='svg':
png_formatter.type_printers.pop(Figure, None)
svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg'))
+ png_formatter.type_printers.pop(Axes, None)
+ svg_formatter.for_type(Axes, lambda ax: print_axes(ax, 'svg'))
+ png_formatter.type_printers.pop(AxesImage, None)
+ svg_formatter.for_type(AxesImage, lambda ax: print_axes(ax, 'svg'))
raise ValueError("supported formats are: 'png', 'svg', not %r"%fmt)

0 comments on commit bfa368e

Please sign in to comment.