switching between color and grayscale

I am mildly concerned about how my plots will look if

    > someone prints my thesis out in grayscale. Is there an easy
    > way to use the same code with one switch at the beginning to
    > plot in color or grayscale? Is there a way to redefine what
    > happens when I call plot(x,y,'g-') so that 'g-' no longer
    > means green but now means something like color=0.75 so some
    > grayscale specification?

That is an interesting problem I hadn't considered before. We have *a
lot* of colors defined in matplotlib.colors. cnames is a dict from
color names to rgb tuples (see below for example usage).

It would be useful to support custom colors in the rc file or
elsewhere -- then you could do something like

  colors.mygreen : 008000

and later redefine it to gray if you need to

  colors.mygreen : 808080

But you can't do that today... What you can do is manipulate the
cnames dict. I suggest creating a module like mycolors that extends
the cnames dict, and then using these custom color names in your
script. When you want to go grayscale, you just modify the colors in
mycolors.

    # mycolors.py
    from matplotlib.colors import cnames
    if 1: # using color
        cnames['mygreen'] = '#008000'
    else: # using grayscale
        cnames['mygreen'] = '#808080'

Then later, you can use it as follows:

    import mycolors
    from pylab import figure, show, nx
    fig = figure()
    ax = fig.add_subplot(111)
    ax.plot((1,2,3), color='mygreen')
    show()

so you'll only have a single point of maintenance. We'll also accept
a patch to the rc system to support custom colors, which is the
preferred way to go.

JDH

I am making some progress. The following script works, but you have
to shut down ipython if you want to switch from color to grayscale:

from numpy import arange, sin, cos, pi
import matplotlib.colors
reload(matplotlib.colors)
cnames=matplotlib.colors.cnames
from pylab import figure, show, nx, clf, cla
fig = figure(4)
ax = fig.add_subplot(111)
t=arange(0,1,0.01)
y=sin(2*pi*t)
grayscale=0
if not grayscale: # using color
    cnames['mygreen'] = '#008000'
else: # using grayscale
    cnames['mygreen'] = '#808080'

#ax.plot(t,y, color=cnames['green'])
ax.plot(t,y, color='mygreen')
show()

Is there some command to refresh the system color dictionary?

Thanks,

Ryan

···

On 4/17/06, John Hunter <jdhunter@...4...> wrote:

    > I am mildly concerned about how my plots will look if
    > someone prints my thesis out in grayscale. Is there an easy
    > way to use the same code with one switch at the beginning to
    > plot in color or grayscale? Is there a way to redefine what
    > happens when I call plot(x,y,'g-') so that 'g-' no longer
    > means green but now means something like color=0.75 so some
    > grayscale specification?

That is an interesting problem I hadn't considered before. We have *a
lot* of colors defined in matplotlib.colors. cnames is a dict from
color names to rgb tuples (see below for example usage).

It would be useful to support custom colors in the rc file or
elsewhere -- then you could do something like

  colors.mygreen : 008000

and later redefine it to gray if you need to

  colors.mygreen : 808080

But you can't do that today... What you can do is manipulate the
cnames dict. I suggest creating a module like mycolors that extends
the cnames dict, and then using these custom color names in your
script. When you want to go grayscale, you just modify the colors in
mycolors.

    # mycolors.py
    from matplotlib.colors import cnames
    if 1: # using color
        cnames['mygreen'] = '#008000'
    else: # using grayscale
        cnames['mygreen'] = '#808080'

Then later, you can use it as follows:

    import mycolors
    from pylab import figure, show, nx
    fig = figure()
    ax = fig.add_subplot(111)
    ax.plot((1,2,3), color='mygreen')
    show()

so you'll only have a single point of maintenance. We'll also accept
a patch to the rc system to support custom colors, which is the
preferred way to go.

JDH

Is there a similar dictionary for linetypes? I would anticipate
needing to use more linetypes in grayscale than in color (i.e.
everything could be solid in color).

Thanks again,

Ryan

···

On 4/17/06, Ryan Krauss <ryanlists@...287...> wrote:

I am making some progress. The following script works, but you have
to shut down ipython if you want to switch from color to grayscale:

from numpy import arange, sin, cos, pi
import matplotlib.colors
reload(matplotlib.colors)
cnames=matplotlib.colors.cnames
from pylab import figure, show, nx, clf, cla
fig = figure(4)
ax = fig.add_subplot(111)
t=arange(0,1,0.01)
y=sin(2*pi*t)
grayscale=0
if not grayscale: # using color
    cnames['mygreen'] = '#008000'
else: # using grayscale
    cnames['mygreen'] = '#808080'

#ax.plot(t,y, color=cnames['green'])
ax.plot(t,y, color='mygreen')
show()

Is there some command to refresh the system color dictionary?

Thanks,

Ryan

On 4/17/06, John Hunter <jdhunter@...4...> wrote:

>
> > I am mildly concerned about how my plots will look if
> > someone prints my thesis out in grayscale. Is there an easy
> > way to use the same code with one switch at the beginning to
> > plot in color or grayscale? Is there a way to redefine what
> > happens when I call plot(x,y,'g-') so that 'g-' no longer
> > means green but now means something like color=0.75 so some
> > grayscale specification?
>
> That is an interesting problem I hadn't considered before. We have *a
> lot* of colors defined in matplotlib.colors. cnames is a dict from
> color names to rgb tuples (see below for example usage).
>
> It would be useful to support custom colors in the rc file or
> elsewhere -- then you could do something like
>
> colors.mygreen : 008000
>
> and later redefine it to gray if you need to
>
> colors.mygreen : 808080
>
> But you can't do that today... What you can do is manipulate the
> cnames dict. I suggest creating a module like mycolors that extends
> the cnames dict, and then using these custom color names in your
> script. When you want to go grayscale, you just modify the colors in
> mycolors.
>
>
> # mycolors.py
> from matplotlib.colors import cnames
> if 1: # using color
> cnames['mygreen'] = '#008000'
> else: # using grayscale
> cnames['mygreen'] = '#808080'
>
>
> Then later, you can use it as follows:
>
> import mycolors
> from pylab import figure, show, nx
> fig = figure()
> ax = fig.add_subplot(111)
> ax.plot((1,2,3), color='mygreen')
> show()
>
> so you'll only have a single point of maintenance. We'll also accept
> a patch to the rc system to support custom colors, which is the
> preferred way to go.
>
> JDH
>