3
$\begingroup$

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?

$\endgroup$

closed as off-topic by Bob Hanlon, LCarvalho, LLlAMnYP, bbgodfrey, MarcoB Nov 4 '17 at 15:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Bob Hanlon, LCarvalho, LLlAMnYP, bbgodfrey, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Create two plots, combine them with Show. $\endgroup$ – Szabolcs Nov 2 '17 at 11:06
  • $\begingroup$ Ok - What function do I use to assign one colour to my level curves? @Szabolcs $\endgroup$ – Alex Nov 2 '17 at 11:08
  • 1
    $\begingroup$ 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. $\endgroup$ – Szabolcs Nov 2 '17 at 11:12
  • $\begingroup$ Thanks @Szabolcs $\endgroup$ – Alex Nov 2 '17 at 11:12
  • $\begingroup$ Also Contours option to set the levels. $\endgroup$ – Szabolcs Nov 2 '17 at 11:15
4
$\begingroup$

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)"}]

enter image description here

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)

enter image description here

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.