Overloading replot/refigure(2) when running from outside Reinteract environment

23 views
Skip to first unread message

Richard Thomas

unread,
Aug 29, 2012, 11:54:03 AM8/29/12
to reint...@googlegroups.com
I have a desire to be able to run Reinteract worksheets outside of the Reinteract environment - i.e. running the code directly by calling python. This is partly so I can work within Reinteract, but hand code to someone who only has Python installed. With replot, this can be fairly straightforward by ensuring there is a "replot.py" file on the native Python path containing:
====
# Indicate to calling program that we are not using the reinteract version
not_reinteract = True

# Import pylab into base level of "replot", so that for example
# pylab.set_title() can be expressed as replot.set_title()
from pylab import *

# Kludge: this must be called at the start of each new figure, but allows the
# same statement to work in Reinteract or native python
def Axes(*args, **kwargs):
   figure()
   p = axes()
   return p
====

Within the worksheet, if I limit the style of plotting to the following it will work in both environments:

====
import replot
...
p = replot.Axes()
p.semilogx(freq, a_weighting(freq))
p.set_title('A-Weighting Curve')
p.set_xlabel("Frequency (Hz)")
p.grid(True)
p.grid(True, which='minor', color='#55bb22', linewidth=0.5)
p.set_ylabel("Gain (dB)")
p
...
if "not_reinteract" in replot.__dict__:
    replot.show()
====

It would be nice to able to do something similar (or is there a better way?) with refigure2 (assuming I could also get the PDF problem sorted). Through major kludgery I managed to get this working with the original refigure, but have had no success with refigure2. I am very rusty on my Python which doesn't help. Any suggestions?

Richard

Richard Thomas

unread,
Sep 7, 2012, 6:28:39 AM9/7/12
to reint...@googlegroups.com
On Wednesday, 29 August 2012 16:54:04 UTC+1, Richard Thomas wrote:
I have a desire to be able to run Reinteract worksheets outside of the Reinteract environment - i.e. running the code directly by calling python. This is partly so I can work within Reinteract, but hand code to someone who only has Python installed.

Thinking back I realise that my original intention was more to develop code quite interactively using Reinteract, but then run it direct from python (for speed) when I had larger data sets to process. (I'm now back at the interactive development stage on a different project).

Richard

Robert Schroll

unread,
Sep 9, 2012, 5:40:35 PM9/9/12
to reint...@googlegroups.com
On 08/29/2012 11:54 AM, Richard Thomas wrote:
> I have a desire to be able to run Reinteract worksheets outside of the
> Reinteract environment - i.e. running the code directly by calling
> python. This is partly so I can work within Reinteract, but hand code to
> someone who only has Python installed.
[...]
> It would be nice to able to do something similar (or is there a better
> way?) with refigure2 (assuming I could also get the PDF problem sorted).
> Through major kludgery I managed to get this working with the original
> refigure, but have had no success with refigure2. I am very rusty on my
> Python which doesn't help. Any suggestions?

Below is a short library which can work in place of refigure2 with pure
Python. Some caveats:
1) You have to use the 'with figure() as f: ...; f;' syntax instead of
the 'build figure(): ...' syntax, since, obviously, Python doesn't have
the build statement. (The currently-released refigure2 has a hack to
allow the with statement to produce output, but that'll go away in the
upcoming version [Real Soon Now].)
2) You can't use rclocal() or the refigurerc option files. This could
be added in relatively easily, but I didn't feel like it. If you need
this and can't figure it out, please let me know.
3) Figures are displayed for the user one at a time. By adjusting the
__exit__ method, you may be able to change this. (For example, you
could save each figure and move on.)

Hope this helps,
Robert

-----refigure2.py (fake version)---------------------------------------
from matplotlib.figure import Figure as _Figure
from matplotlib.pyplot import *

_Figure.__enter__ = lambda self: self
_Figure.__exit__ = lambda self, type, value, traceback: show()
-----------------------------------------------------------------------

Reply all
Reply to author
Forward
0 new messages