refigure2 — Output Manipulation

A patch to Reinteract allows extensions to manipulate the output that goes to the notebook. refigure2 can take advantage of this ability to solve two annoyances.

The Problem

This example illustrates two annoyances in the design of refigure2.

with figure() as f:1
    plot([1,2,1])
    f1
[<matplotlib.lines.Line2D object at 0x9f792ec>]2

  1. The figure must be named at the beginning of the with block and then called at the end, in order to have the figure actually display. It would be better for refigure2 to make that happen automatically.
  2. The plotting commands return the objects that were plotted. This output is printed, but is generally not useful.

The Patch

Both of these problems could be fixed if extensions could manipulate the output that goes to Reinteract. To allow this, Owen Taylor wrote this patch for Reinteract. It has yet to be added to the trunk, so you will have to apply it yourself to get the new behavior. (If you're using git, you'll need the git am command.) This isn't necessary — refigure2 will continue to work with the stock Reinteract. But you'll need it to get the following behavior.

The Solution

When the patch is applied, refigure2 does two things differently:

  1. The figure is automatically output into the notebook at the end of a with block.
  2. The output of return values from commands in the with block may be suppressed by setting rcParams['refigure.disableoutput'] = True or passing the disable_output=True keyword pair to figure(). Note that print statements still work.
with figure(disable_output=True):
    plot([1,2,1])
    print "Only the output I want!"
Only the output I want!

This second feature only disables the display of the return values; the plot commands still return the same values, so you can still do lines = plot(...).

If the prtining of output is not suppressed, old worksheets may display odd behavior, with two figure widgets output at the same time. The easiest way to fix this is to set the refigure.disableoutput rc setting to True. This causes the figure returned by the old code to be ignored, leaving you with only the figure produced by the with block. Personally, I recommend applying this setting in one of the global refigurerc files (in the same directory as refigure2.py or in ~/.matplotlib) and then not worrying about it.

Robert Schroll, rschroll at gmail dot com