5
$\begingroup$

I have the following overlapped contour plot

cplot=Show[
  ContourPlot[
    f[x, y], {x, -1, 1}, {y, -1, 1},  
    Contours->15, ContourShading -> None,ContourLabels -> None,
    ContourStyle ->Blue,
    PlotLegends->{"\[ScriptL]₁(x, y) = x\.b3+xy"}
  ],
  ContourPlot[
    g[x, y], {x, -1, 1}, {y, -1, 1}, 
    Contours->15,ContourShading -> None,  ContourLabels -> None,
    ContourStyle ->Red, 
    PlotLegends->{"\[ScriptL]₂(x, y) = \[Minus]xy"}
  ]
]

I want to add information about the value of the level sets by either:

  • changing the alpha value of the contour lines (more transparent = larger value)
  • changing the thickness of the contour lines (thicker=smaller value)
  • changing the style: thick|normal|dashed|dotted (small -> large)

enter image description here

$\endgroup$

1 Answer 1

5
$\begingroup$

You can use a custom DisplayFunction to modify styling of contour lines based on contour levels (using the tooltip labels to identify the contour level for each line):

ClearAll[displayFunction]
displayFunction[ordering_: ReverseSort, range_: {.1, 1}, 
   directives_: {AbsoluteThickness[3 #] &, AbsoluteDashing[3 #] &, Opacity}] := 
  Module[{labels = Rescale[#, MinMax @ #, range] & @
       ordering@Cases[#, Tooltip[a_, l_] :> l, All]}, 
    ReplaceAll[l_Line :> With[{lbls = Last[labels = RotateLeft[labels]]}, 
     {## & @@ Through[directives @ lbls], l}]] @ #] &;

Examples:

ClearAll[f, g]
f[x_, y_] := x^3 + x y;
g[x_, y_] := - x y;

With default arguments displayFunction[] modifies thickness, dashing and opacity directives based on contour levels in descending order:

Show[MapThread[
  ContourPlot[#[x, y], {x, -1, 1}, {y, -1, 1}, 
    Contours -> 15, 
    ContourStyle -> #2, 
    PlotLegends -> #3,
    ContourShading -> None,
    DisplayFunction -> displayFunction[]] &, 
 {{f, g}, {Red, Blue}, {{x^3 + x y}, {- x y}}}]]

enter image description here

Use displayFunction[Sort] to make higher contour levels rendered with larger thickness/dashing/opacity:

enter image description here

Use displayFunction[ReverseSort, {.1, 1}, {Opacity}] to change only opacity:

enter image description here

Use displayFunction[ReverseSort, {.1, 1}, {AbsoluteThickness[5 #] &}] to change only thickness:

enter image description here

Use displayFunction[ReverseSort, {.1, .8}, {AbsoluteDashing[10 #] &}] to modify dashing only:

enter image description here

You can use the last argument of displayFunction to inject arbitrary styling directives. For example, use

displayFunction[ReverseSort, {.1, .8}, {AbsoluteThickness[3] &, Dashed &,
  Dynamic @ Blend[{CurrentValue["Color"], Yellow}, #] &}]

to make the contour lines thick and dashed and blend the contour color with Yellow based on contour levels:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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