How to color the same contour line with different colors according y value
If it is acceptable to order the lines by the y value of the first point in each contour we can use a modification of Pickett's method:
plot = Show[{ContourPlot[Sin[y - x^2] == 0, {x, -1, 0}, {y, -10, 10}],
ContourPlot[Sin[-y - x^2] == 0, {x, 0, 1}, {y, -10, 10}]}, PlotRange -> All];
cols = {Red, Black, Blue, Orange, Green, Pink, Brown};
Normal[plot] /. {a___, l : Longest[Line[_] ..], b___} :>
{a, Riffle[cols, {l} ~SortBy~ Extract[{1, 1, 2}] ], b}
Or using xslittlegrass's restylePlot2
as the base method:
restyleWithSort[p_, op : OptionsPattern[ListLinePlot]] :=
ListLinePlot[Cases[Normal@p, Line[x__] :> x, ∞] ~SortBy~ Extract[{1, 2}],
op, Options[p]]
Show[restyleWithSort /@ {
ContourPlot[Sin[y - x^2] == 0, {x, -1, 0}, {y, -10, 10}],
ContourPlot[Sin[-y - x^2] == 0, {x, 0, 1}, {y, -10, 10}]
},
PlotRange -> All
]
You can manipulate the expression manually:
plot = ContourPlot[
Sin[y - x^2] == 0, {x, -1, 1}, {y, -10, 10}
];
plot /. {a___, l : Longest[Line[_] ..], b___} :>
{a, Riffle[Array[ColorData[97], Length@{l}], {l}], b}
In order to find the pattern it helps to look at plot // FullForm
. ColorData[97]
is the list of default colors for Mathematica 10. Here is another answer I wrote two days ago, which also shows how this technique can be used.
func[cplot_, cf_, s_] := With[{cp = cplot},
pt = cp[[1, 1]];
lines = Cases[cp, Line[x__] :> x, -1];
max = Max[#[[All, 2]]] & /@ (Part[pt, #] & /@ lines);
Show[Graphics[
GraphicsComplex[pt,
MapThread[{cf[Abs[Rescale[#1, {Min[max], Max[max]}] - s]], Thick,
Line[#2]} &, {max, lines}]], Frame -> True,
AspectRatio -> Full]]
]
For this example:
p1 = ContourPlot[Sin[y - x^2] == 0, {x, -1, 0}, {y, -10, 10}];
p2 = ContourPlot[Sin[-y - x^2] == 0, {x, 0, 1}, {y, -10, 10}];
Manipulate[
Show @@ (func[#, color, s] & /@ {p1, p2}), {{color,
Hue}, {Hue -> "Hue", ColorData["Rainbow"] -> "Rainbow",
ColorData["Pastel"] -> "Pastel"}}, {{s, 0, "invert"}, {0, 1}}]