switching output formats within a function

Hi

I'm writing a small script to plot my data, and I'd like to use a command
line option to allow the same plot to be either displayed with GTK or
output to postscript.

This means I have to switch matplotlib backends within the script.

Now by the time I know what option the user has chosen, I'm in a function:

def plotThings(options):
    matplotlib.use(options.plotbackend)
    from matplotlib.matlab import *

    plot(...)
    ...

and python complains

SyntaxWarning: import * only allowed at module level

It still works, but I think I'm on thin ice. And it doesn't work if I do:

def main():
    # ... parse options ...

    matplotlib.use(options.plotbackend)
    from matplotlib.matlab import *

    plotThings(options)

SyntaxWarning: import * only allowed at module level
NameError: global name 'plot' is not defined

The matplotlib docs say you need to specify the backend before importing
matplotlib.matlab. But this seems a bit restrictive: what if I want to
display a plot on screen, and then output the same plot to postscript and
print it? Normally imports are done only once at the top of a file, but
I'd like to be able to switch backends anywhere.

What are your thoughts on this issue?

I've been using matplotlib for a while. It's the best python plotting tool
I reckon. Thanks for contributing to free software.

Cheers and thanks,
Matthew.

Well I just answered my own question. As the docs point out, you can use
the -dPS option to turn on the postscript backend. So to use this in
harmony with my scripts I needed to:

a) break out the plotting into a separate application called using command
line options (conveniently I had already done this)

b) tell my option parser about the -d option:

import matplotlib
from matplotlib.matlab import *

# ...

def main():
    # ...
    parser.add_option("-d", dest="plotbackend", default="GTK",
                        choices=matplotlib._knownBackends.keys(),
                        help="Graphics backend to use to generate plots.")
    # ...

c) use options.plotbackend in my own code to tell whether to
savefig('something.ps')

It's a bit awkward, but workable.

Cheers,
Matthew.

···

On Mon, 5 Jan 2004, matthew arnison wrote:

Hi

I'm writing a small script to plot my data, and I'd like to use a command
line option to allow the same plot to be either displayed with GTK or
output to postscript.

This means I have to switch matplotlib backends within the script.

Now by the time I know what option the user has chosen, I'm in a function:

def plotThings(options):
    matplotlib.use(options.plotbackend)
    from matplotlib.matlab import *

    plot(...)
    ...

and python complains

SyntaxWarning: import * only allowed at module level

It still works, but I think I'm on thin ice. And it doesn't work if I do:

def main():
    # ... parse options ...

    matplotlib.use(options.plotbackend)
    from matplotlib.matlab import *

    plotThings(options)

SyntaxWarning: import * only allowed at module level
NameError: global name 'plot' is not defined

The matplotlib docs say you need to specify the backend before importing
matplotlib.matlab. But this seems a bit restrictive: what if I want to
display a plot on screen, and then output the same plot to postscript and
print it? Normally imports are done only once at the top of a file, but
I'd like to be able to switch backends anywhere.

What are your thoughts on this issue?

I've been using matplotlib for a while. It's the best python plotting tool
I reckon. Thanks for contributing to free software.

Cheers and thanks,
Matthew.

-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
Free Linux Tutorials. Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users