using plotting contexts

Is there a means of using alternate rcParams dictionaries

    > for use with different backends, say as a parameter to the
    > print_figure method? It doesn't look like it at the
    > moment. I'd guess to do that now means replacing rcParams
    > temporarily, rendering the plot to the file, and resetting
    > rcParams to what it was (I haven't actually tried it), but
    > that seems clumsier than it should be.

There are a few ways to go here.

Probably the best is based on the answer to "How do I customize the
default behavior of a single figure?"
http://matplotlib.sourceforge.net/faq.html#CUSTOM

Ie, you can set the rc params for a given session by updating the rc
dict before importing matplotlib. You could set up a module like
plot_contexts

plot_contexts.py::

from matplotlib import rcParams

def use(s):
    if s == 'powerpoint':
        rcParams['figure.facecolor'] = 'b'
        rcParams['text.fontname'] = 'cmr10'
        rcParams['lines.linewidth'] = 2.0
    elif s = 'some journal'
        and so on

And at the script level you could do

import plot_contexts
plot_contexts.use('powerpoint')
from matplotlib.matlab import *

The one down side of the rcParams approach is that currently all the
object defaults are evaluated at module load time, so you can only
switch at the start of your python session. Eg, the current
constructors look like

class Line2D(Artist):
    def __init__(self, xdata, ydata,
                 linewidth=rcParams['lines.linewidth'],
                 linestyle=rcParams['lines.linestyle'],
                 )

By changing all the constructor defaults to

    def __init__(self, xdata, ydata,
                 linewidth=None,
                 linestyle=None,
                 ):
                 if linewidth is None: linewidth = rcParams['lines.linewidth'],
                 if linestyle is None: linestyle = rcParams['lines.linestyle'],

we could defer those evaluations to construction time, so you could
switch contexts in the middle of an interactive session

  >>> plot_contexts.use('power point')
  >>> make_some_figs()
  >>> plot_contexts.use('some journal')
  >>> make_some_figs() # again

I already did this for the FontProperties so you can have context
fonts, as in examples/font_properties_demo.py. It wouldn't be much
work to do the same for the other properties controlled by rc.

As another approach, I have considered making the matplotlibrc finder
check the current dir first, and using that file if it finds one.
Then you could have dir based contexts.

JDH

John Hunter wrote:

    > Is there a means of using alternate rcParams dictionaries
    > for use with different backends, say as a parameter to the
    > print_figure method? It doesn't look like it at the
    > moment. I'd guess to do that now means replacing rcParams
    > temporarily, rendering the plot to the file, and resetting
    > rcParams to what it was (I haven't actually tried it), but
    > that seems clumsier than it should be.

There are a few ways to go here.

Probably the best is based on the answer to "How do I customize the
default behavior of a single figure?"
http://matplotlib.sourceforge.net/faq.html#CUSTOM

I had looked at that. I'm a little confused by the terminology though.
This sounds like a prescription for changing the defaults for some
parameters for a session rather than for a single figure. Do I
misread that? (Also, on my browser these entries are much wider
than the browser window for some reason; later in the faq that changes
as do some of the fonts. I wonder if something is screwed up there).

[details about setting up session context and object intialization
snipped]

I'm thinking of the case where someone tailors plots interactively
and then wants to render the same plot, but with different defaults
for a png file or postscript in the same session. In that case
a session change of default isn't very useful. They basically are
going to be force to write a script, and run it from a different
session to get a different version. So I think that is going
to be somewhat inconvenient for some users.

Changing the use of the rcParams to be immediate rather than locked
in at module initialization is more attractive to me (and may allow
one to assign differnt rc sets to different backends by default as
well; but that is not urgent). Do you mind changing when the
parameters are picked up? (I'm asking that you do it now, just
whether that is acceptable).

Thanks, Perry