Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Color coding a Table entry

4 views
Skip to first unread message

AES

unread,
Oct 8, 2008, 6:27:03 AM10/8/08
to
I'm Printing (to the screen) a Table like, let's say,

Table[{f1, f2, f3}, (x, xmin, xmax}]

and anytime the value of f2 goes negative I want that value printed in
Red.

What's a simple, compact way to code this?

Szabolcs Horvat

unread,
Oct 9, 2008, 6:31:58 AM10/9/08
to
AES wrote:
> I'm Printing (to the screen) a Table like, let's say,
>
> Table[{f1, f2, f3}, (x, xmin, xmax}]
>
> and anytime the value of f2 goes negative I want that value printed in
> Red.
>

Before displaying the result on screen, replace each negative value:

{1, 2, -1, -2} /. x_?Negative -> Style[x, Red]

David Park

unread,
Oct 9, 2008, 6:32:29 AM10/9/08
to
Table[Style[i, If[i < 0, Red, Black]], {i, -2, 2}]

--
David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/


"AES" <sie...@stanford.edu> wrote in message
news:gci1tn$ji$1...@smc.vnet.net...

Meng Lu

unread,
Oct 9, 2008, 6:32:51 AM10/9/08
to
AES wrote:
> I'm Printing (to the screen) a Table like, let's say,
>
> Table[{f1, f2, f3}, (x, xmin, xmax}]
>
> and anytime the value of f2 goes negative I want that value printed in
> Red.
>
> What's a simple, compact way to code this?
>
>
One way is

Print[Table[{Sin[x], If[# < 0, Style[#, Red], #] &@Cos[x], x}, {x, 1,
10}]]

{{Sin[1],Cos[1],1},{Sin[2],Cos[2],2},{Sin[3],Cos[3],3},{Sin[4],Cos[4],\
4},{Sin[5],Cos[5],5},{Sin[6],Cos[6],6},{Sin[7],Cos[7],7},{Sin[8],Cos[\
8],8},{Sin[9],Cos[9],9},{Sin[10],Cos[10],10}}


Bob Hanlon

unread,
Oct 9, 2008, 6:33:41 AM10/9/08
to
Table[{f1, Style[f2, If[f2 < 0, Red, Black]], f3}, {x, -1, 5}]


Bob Hanlon

---- AES <sie...@stanford.edu> wrote:

=============


I'm Printing (to the screen) a Table like, let's say,

Table[{f1, f2, f3}, (x, xmin, xmax}]

and anytime the value of f2 goes negative I want that value printed in
Red.

What's a simple, compact way to code this?


--

Bob Hanlon


Albert Retey

unread,
Oct 9, 2008, 6:33:52 AM10/9/08
to

>From other posts I believe you don't actually appreciate "functional"
programming constructs to much. Anyway, this problem is a good
demonstration how much simpler and clearer this can be than writing
loops. Note that I tried to avoid arcane shortcuts like # and & for
the formulation of the pure function:

Map[
Function[{x}, If[x < 0, Style[x, Red], x]],
Table[RandomReal[{-1, 1}, {3}], {x, 1, 3}],
{2}
]

Of course for presentation purposes you might want to wrap the result
with a Grid or MatrixForm.

hth,

albert

Albert Retey

unread,
Oct 9, 2008, 6:34:02 AM10/9/08
to

correcting my previous post: pattern matching is probably even better in
this case:

ReplaceAll[


Table[RandomReal[{-1, 1}, {3}], {x, 1, 3}],

x_?Negative :> Style[x, Red]
]

Jean-Marc Gulliet

unread,
Oct 9, 2008, 6:35:58 AM10/9/08
to
AES wrote:

One possible way:

f1 := RandomInteger[{-1, 1}];
f2 := (-1)^x;
f3 := Sin[N[x]];

redneg[expr_] := If[expr < 0, Style[expr, Red], expr, expr]

With[{xmin = 0, xmax = 10},
Table[{f1, redneg[f2], f3}, {x, xmin, xmax}]]

Regards,
-- Jean-Marc

sjoerd.c...@gmail.com

unread,
Oct 9, 2008, 6:36:21 AM10/9/08
to
Map[If[# < 0, Style[#, Red], #] &, Table[{f1, f2, f3}, (x, xmin,
xmax}], {-1}]

Cheers -- Sjoerd

Bill Rowe

unread,
Oct 9, 2008, 6:37:18 AM10/9/08
to
On 10/8/08 at 6:26 AM, sie...@stanford.edu (AES) wrote:

>I'm Printing (to the screen) a Table like, let's say,

>Table[{f1, f2, f3}, (x, xmin, xmax}]

>and anytime the value of f2 goes negative I want that value printed
>in Red.

>What's a simple, compact way to code this?

Define a function to color as you like. For example,

f[x_] := Style[x, Red] /; x < 0
f[x_] := x /; x >= 0

Now

Table[{f1, f@f2, f3}, (x, xmin, xmax}]

will have all negative values of f2 red and all other entries the usual col=
or.

Alexei Boulbitch

unread,
Oct 9, 2008, 6:38:01 AM10/9/08
to
I'm Printing (to the screen) a Table like, let's say,

Table[{f1, f2, f3}, (x, xmin, xmax}]

and anytime the value of f2 goes negative I want that value printed in
Red.

What's a simple, compact way to code this?


Try this (here random numbers instead of your terms are to be precise)

TableForm[
Table[{f1, Style[ f2, If[f2 >= 0, Black, Red]],
f3} /. {f1 -> RandomReal[],
f2 -> (-1)^RandomInteger[]*RandomReal[], f3 -> RandomReal[]}, {i,
0, 5}]]


--
Alexei Boulbitch, Dr., Habil.
Senior Scientist

IEE S.A.
ZAE Weiergewan
11, rue Edmond Reuter
L-5326 Contern
Luxembourg

Phone: +352 2454 2566
Fax: +352 2454 3566

Website: www.iee.lu

This e-mail may contain trade secrets or privileged, undisclosed or otherwise confidential information. If you are not the intended recipient and have received this e-mail in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal from your system. Thank you for your co-operation.

Jens-Peer Kuska

unread,
Oct 9, 2008, 6:39:59 AM10/9/08
to
Hi,

If[# < 0, Style[#, Red], #] & /@ Table[RandomReal[{-1, 1}], {10}]

Regards
Jens

AES

unread,
Oct 10, 2008, 4:39:17 AM10/10/08
to
Original post:

> I'm Printing (to the screen) a Table like, let's say,
>
> Table[{f1, f2, f3}, (x, xmin, xmax}]
>
> and anytime the value of f2 goes negative I want that value printed in
> Red. What's a simple, compact way to code this?

Of the dozen or so replies, I'd say that

> Table[{f1, Style[f2, If[f2 < 0, Red, Black]], f3}, {x, -1, 5}]
>
>
> Bob Hanlon

is by far the best: simple, clean, you can see what it does, no arcane
symbols, no cross effects on anything else outside that entry (and, the
solution I'd already come up with on my own).

If the conditional in the If[ ] statement got lengthy, to avoid
cluttering up the Table with it I might go to the alternative suggestion

> redneg[expr_] := If[cond[expr], Style[expr, Red], expr, expr]


>
> With[{xmin = 0, xmax = 10},
< Table[{f1, redneg[f2], f3}, {x, xmin, xmax}]]
<

> Regards, Jean-Marc

Or, just define the conditional separately, outside the Table,

cond[expr_] : = <<something>>

and use

Style[f2, If[fcond[f2], Red, Black]]

in the Table.

Would Plot[redneg[f[x]], {x, xmin, xmax}] make the plot turn black
below the x axis??? Have to try it and see . . .

Bob Hanlon

unread,
Oct 11, 2008, 6:42:12 AM10/11/08
to
For plots use ColorFunction

Plot[(x - 1) (x - 4), {x, 0, 5},
ColorFunction -> Function[{x, y}, If[y < 0, Blue, Red]],
ColorFunctionScaling -> False]


Bob Hanlon

---- AES <sie...@stanford.edu> wrote:

=============
Original post:

cond[expr_] : = <<something>>

and use

in the Table.


--

Bob Hanlon


Bill Rowe

unread,
Oct 11, 2008, 6:46:42 AM10/11/08
to
On 10/10/08 at 4:36 AM, sie...@stanford.edu (AES) wrote:

>Original post:

>>redneg[expr_] := If[cond[expr], Style[expr, Red], expr, expr]

>Would Plot[redneg[f[x]], {x, xmin, xmax}] make the plot turn black


>below the x axis??? Have to try it and see . . .

No, this will simply generate an error message telling you the
Plot has no idea what to do with functions that do not return
numeric values. Plotting color is set by specifying a PlotStyle.
To get different plot colors for positive/negative values you
would need to define a custom ColorFunction to set the plot color.

Yves Klett

unread,
Oct 13, 2008, 7:05:24 AM10/13/08
to
AES,

you could use Style in many different ways, e.g.

Table[If[i < 0, Style[i, Red], Style[i, Green]], {i, -5, 5}]

Regards,
Yves


AES schrieb:

0 new messages