# ContourPlot - how to assign specific colours to level curves? [closed]

I would like to plot some level curves of two functions $f(x,y)$ and $g(x,y)$ of two variables $x$, $y$, on ONE contour plot.

For example $f = \sqrt{x}( 5 + \sin y)$ and $g = \sqrt{x\left[(5+ \sin y)^{2} - (2 - \cos y)^{2}\right]}$.

I have the level curves

$f(x,y) = 10, \quad f(x,y) = 15, \quad f(x,y) = 20$

and

$g(x,y) = 5, \quad g(x,y) = 7, \quad g(x,y) = 10$

I want to set ALL the level curves of $f(x,y)$ one colour (say red), and ALL the level curves of $g(x,y)$ another colour (blue).

I know I can use the ContourPlot command, but how do I set the colours of the curves in the specified way?

• Create two plots, combine them with Show. – Szabolcs Nov 2 '17 at 11:06
• Ok - What function do I use to assign one colour to my level curves? @Szabolcs – Alex Nov 2 '17 at 11:08
• You will always find the answer to plot styling questions in the documentation of the given function. Check the examples for the ContourShading and ContourStyle options in the ContourPlot doc page. – Szabolcs Nov 2 '17 at 11:12
• Thanks @Szabolcs – Alex Nov 2 '17 at 11:12
• Also Contours option to set the levels. – Szabolcs Nov 2 '17 at 11:15

You can use ContourStyle to set the colours of the plots, and put a list of the functions and countours you want to plot in the first argument.

f[x_, y_] := Sqrt[x] (5 + Sin[y])
g[x_, y_] := Sqrt[x ((5 + Sin[y])^2 - (2 - Cos[y])^2)]

ContourPlot[{f[x, y] == {10, 15, 20}, g[x, y] == {5, 7, 10}},
{x, 0, 10}, {y, -5, 5}, ContourStyle -> {Red, Blue}, PlotLegends -> {"f(x,y)", "g(x,y)"}] This approach, while convenient, has a number of problems, and I get the sense that it's not fully implemented. For example, tooltips get messed up and show the entirety of {f[x, y] == {10, 15, 20}, g[x, y] == {5, 7, 10}} over every contour. ContourLabels doesn't work as advertised (or at all, in any useful way I could fine).

Rather than trying to come up with workaraounds for all these issues, I'll just post a convenient(ish) alternative using Show (as suggested by @Szabolcs in comments).

If you define a list of functiondata

functiondata = {{f, {10, 15, 20}, Red}, {g, {5, 7, 10}, Blue}};


(and you can of course change this to include more functions, more tuneable options, etc...), then you can get a nice plot with

Show @@ (ContourPlot[#1[x, y], {x, 0, 10}, {y, -5, 5},
ContourStyle -> #3, Contours -> #2, ContourShading -> None,
PlotLegends -> SwatchLegend[{#3}, {ToString[#1] <> "(x,y)"}]] & @@@
functiondata) which has all of its functioniality (tooltips, options working, etc.) intact, as far as I can tell.

You could build your own contour plotting function to satisfy all your requirements. Doing that properly would be a different question, but you could get a quick and dirty (but easy and working) verion like this:

multiContourPlot[fd_] :=
Show @@ (ContourPlot[#1[x, y], {x, 0, 10}, {y, -5, 5},
ContourStyle -> #3, Contours -> #2, ContourShading -> None,
PlotLegends ->
SwatchLegend[{#3}, {ToString[#1] <> "(x,y)"}]] & @@@ fd)


so that

multiContourPlot[{{f, {10, 15, 20}, Red}, {g, {5, 7, 10}, Blue}}]


would produce the above plot.