3
$\begingroup$

Can someone suggest a way to specify per-function MeshStyle when plotting multiple functions? An example is below, I'm trying to show two contours, with black and red/dashing style. The Plot3D only displays a single style

a = {{1, 0}, {0, 2}};
b = {{3/2, 1}, {1, 6}};
f1[x_, y_] = {x, y}.a.{x, y};
f2[x_, y_] = {x, y}.b.{x, y};
{r1, r2} = Eigenvalues[{b, a}];
contourStyle1 = Black;
contourStyle2 = Directive[Red, Dashed];
plot3d = Plot3D[{r1 f1[x, y], f2[x, y]}, {x, -2, 2}, {y, -2, 2}, 
   PlotRange -> {0, 30}, ClippingStyle -> None, 
   PlotStyle -> Opacity[.5], MeshFunctions -> {#3 &}, Mesh -> {{2}}, 
   MeshStyle -> {contourStyle1, contourStyle2}];
contours1 = 
  ContourPlot[{r1 f1[x, y]}, {x, -2, 2}, {y, -2, 2}, Contours -> {2}, 
   ContourShading -> None, ContourStyle -> contourStyle1];
contours2 = 
  ContourPlot[{f2[x, y]}, {x, -2, 2}, {y, -2, 2}, Contours -> {2}, 
   ContourShading -> None, ContourStyle -> contourStyle2];
GraphicsRow[{plot3d, Show[contours1, contours2]}]

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ use MeshFunctions -> {ConditionalExpression[#3, #3 == r1 f1[#, #2]] &, ConditionalExpression[#3, #3 == f2[#, #2]] &}, Mesh->{{2}, {2}}, and large values for PlotPoints (e.g., 300) and MaxRecursion (say, 6) $\endgroup$
    – kglr
    Aug 12 at 11:38
  • 2
    $\begingroup$ or, plot r1 f1[x,y] and f2[x,y] separately and combine with Show: that is, plot3d = Show[MapThread[Plot3D[#, {x, -2, 2}, {y, -2, 2}, PlotRange -> {0, 30}, ClippingStyle -> None, PlotStyle -> Opacity[.5, ColorData[97]@#2], MeshFunctions -> {#3 &}, Mesh -> {{2}}, MeshStyle -> #3] &, {{r1 f1[x, y], f2[x, y]}, {1, 2}, {contourStyle1, contourStyle2}}]] $\endgroup$
    – kglr
    Aug 12 at 11:44
7
$\begingroup$

1. Plot r1 f1[x, y] and f2[x, y] separately and combine with Show:

Show[MapThread[
  Plot3D[#, {x, -2, 2}, {y, -2, 2}, 
    PlotRange -> {0, 30}, 
    ClippingStyle -> None, 
    PlotStyle -> Directive[Opacity[.5], #2], 
    MeshFunctions -> {#3 &}, Mesh -> {{2}}, 
    MeshStyle -> #3] &, 
   {{r1 f1[x, y], f2[x, y]}, 
   Take["DefaultPlotStyle" /. 
     (Method /. Charting`ResolvePlotTheme[Automatic, Plot3D]), 2], 
   {contourStyle1, contourStyle2}}]]

enter image description here

2. Use {ConditionalExpression[#3, #3 == r1 f1[#, #2]] &, ConditionalExpression[#3, #3 == f2[#, #2]] &} as MeshFunctions and use large values for PlotPoints and MaxRecursion:

Plot3D[{r1 f1[x, y], f2[x, y]}, {x, -2, 2}, {y, -2, 2}, 
 PlotPoints -> 300,
 MaxRecursion -> 6,
 PlotRange -> {0, 30}, 
 ClippingStyle -> None, 
 PlotStyle -> Opacity[.5], 
 MeshFunctions -> {ConditionalExpression[#3, #3 == r1 f1[#, #2]] &, 
   ConditionalExpression[#3, #3 == f2[#, #2]] &}, 
 Mesh -> {{2}, {2}}, 
 MeshStyle -> {contourStyle1, contourStyle2}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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