k1=xx; k2=yy; k3=zz;
myPlotColor[i_] = RBGColor[1 - k1 i, 1 - k2 i, 1 - k3 i]
with k1, k2, k3 each set equal to either 0 or 1, and then use
ColorFunction->myPlotColor
in the plot command, I get just the results I want. For example if I
execute the seven lines (either in a single cell or as separate cells)
k1 = 0; k2 = 1; k3 = 1;
red[i_] = RGBColor[1 - k1 i, 1 - k2 i, 1 - k3 i];
gCP = ContourPlot[(x + y)/2, {x, 0, 1}, {y, 0, 1},
ColorFunction -> red];
k1 = 1; k2 = 0; k3 = 1;
green[i_] = RGBColor[1 - k1 i, 1 - k2 i, 1 - k3 i];
gDP = DensityPlot[(x + y)/2, {x, 0, 1}, {y, 0, 1},
ColorFunction -> green];
Show[GraphicsArray[{{gCP, gDP}}]];
I get the beautiful red ContourPlot gCP that I want from lines 1 thru
3; the beautiful green DensityPlot gDP that I want from lines 4 thru
6; and then the two of them together from line 7.
What puzzles me, however, is that if I replace the string "green" by
"red" in lines 5 and 6 above, the individual plots CP and DP from lines
1 thbru 6 still come out red and green as before --- but the two
graphics produced by the Show[ ] command in line 7 now both come out
green.
The DP resulting from line 6 obviously knew it was supposed to be green
when it was first rendered, even though its ColorFunction was
confusingly named "red", because red[i_] had been redefined using k
values appropriate to green in line 5. How come it forgets all about
this when Show time comes around?
[Email cc of replies welcomed]
come out green**.
obvioulsy, gCP and gDP refer to the symbols red and green. Therefore,
when you redefine red as the color green, you will get what you asked for.
Daniel
ContourPlot outputs a ContourGraphics object. You can check by using
FullForm that this object still contains ColorFunction->red. The
colour function is used when the graph is drawn, not when the
ContourGraphics object is generated.
You can convert it to an "expanded" graphics object by
gCP = Graphics[gCP]
In this new object the colours are hard-coded and they will not change
if you redefine red[].
Tip: use Hue[0, #, 1]& as your colour function. You can easily vary
the colour by changing the first parameter in Hue.
Szabolcs
The behavior you describe is expected. Evaluate the following
expressions and look at the full form of, say, gCP.
gCP is a graphic object, which has been evaluated on line 3. You can
notice that the structure of a graphic object is a list of points (or
list of lists, etc), points that are not going to be evaluated again,
and a list of options in the form of transformation rules, options that
will be evaluated every time you ask to draw the plot.
Therefore, the first time the plot is drawn, the current definition of
the function red is the one displayed in line 2b. Then, this definition
is changed by the one displayed on line 5b. (Note that at this stage
Mathematica has forgotten everything about the previous definition of red.)
When you call the function Show on line 7, the options of gCP are
evaluated again, and, this time, the current definition of red is the
one displayed on line 5b. Consequently, both graphics call the same
current, and only existing, definition of the function red, which colors
the plots in green.
(* lines 1 and 2 *)
k1 = 0; k2 = 1; k3 = 1;
red[i_] = RGBColor[1 - k1*i, 1 - k2*i, 1 - k3*i];
(* line 2b *)
Information["red", LongForm -> False]
(* line 3*)
gCP = ContourPlot[(x + y)/2, {x, 0, 1}, {y, 0, 1},
ColorFunction -> red];
(* lines 4 and 5 *)
k1 = 1; k2 = 0; k3 = 1;
red[i_] = RGBColor[1 - k1*i, 1 - k2*i, 1 - k3*i];
(* line 5b *)
Information["red", LongForm -> False]
(* lines 6 and 7 *)
gDP = DensityPlot[(x + y)/2, {x, 0, 1}, {y, 0, 1},
ColorFunction -> red];
Show[GraphicsArray[{{gCP, gDP}}]];
(* line 8 *)
FullForm[gCP]
Hope this helps,
Jean-Marc
The behavior you describe is expected. Evaluate the following
expressions and look at the full form of, say, gCP.
gCP is a graphic object, which has been evaluated on line 3. You can
notice that the structure of a graphic object is a list of points (or
list of lists, etc), points that are not going to be evaluated again,
and a list of options in the form of transformation rules, options that
will be evaluated every time you ask to draw the plot.
Therefore, the first time the plot is drawn, the current definition of
the function red is the one displayed in line 2b. Then, this definition
is changed by the one displayed on line 5b. (Note that at this stage
Mathematica has forgotten everything about the previous definition of red.)
When you call the function Show on line 7, the options of gCP are
evaluated again, and, this time, the current definition of red is the
one displayed on line 5b. Consequently, both graphics call the same
current, and only existing, definition of the function red, which colors
the plots in green.
(* lines 1 and 2 *)
k1 = 0; k2 = 1; k3 = 1;
red[i_] = RGBColor[1 - k1*i, 1 - k2*i, 1 - k3*i];
(* line 2b *)
Information["red", LongForm -> False]
(* line 3*)
gCP = ContourPlot[(x + y)/2, {x, 0, 1}, {y, 0, 1},
ColorFunction -> red];
(* lines 4 and 5 *)
k1 = 1; k2 = 0; k3 = 1;
red[i_] = RGBColor[1 - k1*i, 1 - k2*i, 1 - k3*i];
(* line 5b *)
Information["red", LongForm -> False]
(* lines 6 and 7 *)
gDP = DensityPlot[(x + y)/2, {x, 0, 1}, {y, 0, 1},