Test plot:
cplot = ContourPlot[y^2 - x^3 + x^4 == 0, {x, 0, 1}, {y, -1/2, 1/2}];
With color function scaling.
p1 = cplot /. GraphicsComplex[p_, g_, opts___] :> With[{
pp = Transpose[Rescale /@ Transpose@p], (* ColorFunctionScaling->True *)
cf = ColorData["Rainbow"][#2] &}, (* color function *)
GraphicsComplex[p,
g /. Line[i_] :> Line[i, VertexColors -> cf @@@ pp[[i]]], opts]
]
Without color function scaling. The pp = p
is unnecessary; if it is removed, change the remaining instances of p
and pp
to the same symbol.
p2 = cplot /. GraphicsComplex[p_, g_, opts___] :> With[{
pp = p, (* ColorFunctionScaling -> False *)
cf = ColorData["Rainbow"][0.3 + #2] &},
GraphicsComplex[p,
g /. Line[i_] :> Line[i, VertexColors -> cf @@@ pp[[i]]], opts]
]
With coloring according to scaled arc length. It's advisable to check how many lines were constructed. Each line is colored according to its own scaled arc length measured from whatever point happens to be its first.
Count[cplot, _Line, Infinity]
(* 1 *)
p3 = cplot /. GraphicsComplex[p_, g_, opts___] :> With[{
pp = p, (* ColorFunctionScaling -> False *)
cf = Hue},
GraphicsComplex[
p,
g /. Line[i_] :> Line[
i,
VertexColors ->
cf /@
Rescale@
Accumulate@Prepend[Norm /@ Partition[pp[[i]], 2, 1], 0.]],
opts]
]
Colored according to function of x
and y
.
scalingFN = Exp[Sin[3 #1 + 2 #2]] &;
p4 = cplot /. GraphicsComplex[p_, g_, opts___] :> With[{
pp = Rescale[scalingFN @@@ p], (* Scaled by f[x,y] *)
cf = ColorData["TemperatureMap"]},
GraphicsComplex[p,
g /. Line[i_] :> Line[i, VertexColors -> cf /@ pp[[i]]], opts]
]
Results, in respective order:
Remarks: Note the differences cf @@@ ...
and cf /@ ...
depending on whether cf
is applied to points or to scalars constructed from the points. The code above for color function scaling was given in general. The particular example could be done with pp = Rescale[p[[All, 2]]]
, cf = ColorData["Rainbow"]
, and cf /@ pp
, This code (in its variations) works on GraphicsComplex
only.