convert figure from color to BW/grayscale

Hello,
I need to prepare two versions of figures: color and BW(Black&White).
Is there an easy way to produce just the colored version and than use some command or script to turn it to BW or grayscale?

I thought that converting from color to BW really means: “in all object in the figure, turn any color that is not white to black”.
Is there an easy way to implement this?

Thanks
Eli

···

2010/9/20 Eli Brosh <ebrosh1@...287...>:

Hello,
I need to prepare two versions of figures: color and BW(Black&White).
Is there an easy way to produce just the colored version and than use some
command or script to turn it to BW or grayscale?
I thought that converting from color to BW really means: "in all object in
the figure, turn any color that is not white to black".
Is there an easy way to implement this?

http://github.com/friedrichromstedt/matplotlib

I implemented a RC setting 'gray'. Turn it on by setting either
matplotlib.rcParams['gray'] = True or by setting it in your
matplotlibrc file.

You can also gray out any artist (e.g., the figure), by calling
artist.set_gray(True).

figure = matplotlib.figure.Figure()
... do plotting ...
... save as colour ...
figure.set_gray(True)
... save as b/w ...

If you're using pyplot or pylab, I'm not sure, but I think there is a
function gcf() which gives you the current figure object so that you
can turn gray on there. pylab support should be placed on the TODO
list, I would appreciate feedback on the preferred pylab way, since
I'm not a pylab user at all. I think a pylab function gray(boolean)
would do it.

It's beta, so try it out. It's tested, though.

Friedrich

Hello Friedrich,
I tried your second solution:
figure = matplotlib.figure.Figure()

… do plotting …

… save as colour …

figure.set_gray(True)

… save as b/w …

but it gives me an error message:
fig.set_gray(True)
AttributeError: ‘Figure’ object has no attribute ‘set_gray’

I got the same error message when trying in in pylab as:

fig1=gcf()
fig1.set_gray(True)

and when generating the figure with the command
fig1 = matplotlib.figure.Figure()

I am using matplotlib 0.99.1.1 on ubuntu 10.04/

Is there another way to do it?

Thanks,
Eli

···

On Mon, Sep 20, 2010 at 11:14 AM, Friedrich Romstedt <friedrichromstedt@…287…> wrote:

2010/9/20 Eli Brosh <ebrosh1@…287…>:

Hello,

I need to prepare two versions of figures: color and BW(Black&White).

Is there an easy way to produce just the colored version and than use some

command or script to turn it to BW or grayscale?

I thought that converting from color to BW really means: "in all object in

the figure, turn any color that is not white to black".

Is there an easy way to implement this?

http://github.com/friedrichromstedt/matplotlib

I implemented a RC setting ‘gray’. Turn it on by setting either

matplotlib.rcParams[‘gray’] = True or by setting it in your

matplotlibrc file.

You can also gray out any artist (e.g., the figure), by calling

artist.set_gray(True).

figure = matplotlib.figure.Figure()

… do plotting …

… save as colour …

figure.set_gray(True)

… save as b/w …

If you’re using pyplot or pylab, I’m not sure, but I think there is a

function gcf() which gives you the current figure object so that you

can turn gray on there. pylab support should be placed on the TODO

list, I would appreciate feedback on the preferred pylab way, since

I’m not a pylab user at all. I think a pylab function gray(boolean)

would do it.

It’s beta, so try it out. It’s tested, though.

Friedrich

Eli, the feature is highly experimental, and is available only though fork of matplotlib that Friedrich made in the link he provided you. You would have to install that matplotlib from its source at that link.

Ben Root

···

On Mon, Sep 20, 2010 at 11:13 AM, Eli Brosh <ebrosh1@…287…> wrote:

Hello Friedrich,
I tried your second solution:

figure = matplotlib.figure.Figure()

… do plotting …

… save as colour …

figure.set_gray(True)

… save as b/w …

but it gives me an error message:
fig.set_gray(True)
AttributeError: ‘Figure’ object has no attribute ‘set_gray’

I got the same error message when trying in in pylab as:

fig1=gcf()
fig1.set_gray(True)

and when generating the figure with the command
fig1 = matplotlib.figure.Figure()

I am using matplotlib 0.99.1.1 on ubuntu 10.04/

Is there another way to do it?

Thanks,
Eli

OK,
This worked for me for turning the figure to black and white:
After saving the figure in colors I write

fig=gcf()

for o in fig.findobj(matplotlib.lines.Line2D):
o.set_color(‘k’)

for o in fig.findobj(matplotlib.text.Text):
o.set_color(‘k’)

Than I save it as black and white.

It is beyond my programming skills, but I wish we could have a built in function of this type.

Perhaps as:

Fig_BW=makeBW(fig)

Regards,
Eli

···

On Mon, Sep 20, 2010 at 6:19 PM, Benjamin Root <ben.root@…1304…> wrote:

On Mon, Sep 20, 2010 at 11:13 AM, Eli Brosh <ebrosh1@…287…> wrote:

Hello Friedrich,
I tried your second solution:

figure = matplotlib.figure.Figure()

… do plotting …

… save as colour …

figure.set_gray(True)

… save as b/w …

but it gives me an error message:
fig.set_gray(True)
AttributeError: ‘Figure’ object has no attribute ‘set_gray’

I got the same error message when trying in in pylab as:

fig1=gcf()
fig1.set_gray(True)

and when generating the figure with the command
fig1 = matplotlib.figure.Figure()

I am using matplotlib 0.99.1.1 on ubuntu 10.04/

Is there another way to do it?

Thanks,
Eli

Eli, the feature is highly experimental, and is available only though fork of matplotlib that Friedrich made in the link he provided you. You would have to install that matplotlib from its source at that link.

Ben Root

Eli,

I am glad that worked for you. Ultimately, Friedrich’s approach will make it into a released version of matplotlib so that making a figure into a black and white figure will be something as simple as

fig.set_gray(True)

Which, I hope, will satisfy your needs in the future.

Ben Root

···

On Mon, Sep 20, 2010 at 1:01 PM, Eli Brosh <ebrosh1@…287…> wrote:

OK,
This worked for me for turning the figure to black and white:
After saving the figure in colors I write

fig=gcf()

for o in fig.findobj(matplotlib.lines.Line2D):
o.set_color(‘k’)

for o in fig.findobj(matplotlib.text.Text):
o.set_color(‘k’)

Than I save it as black and white.

It is beyond my programming skills, but I wish we could have a built in function of this type.

Perhaps as:

Fig_BW=makeBW(fig)

Regards,
Eli