2
$\begingroup$

Consider:

f[x_, y_] = 6 - 3 x - 2 y;
GraphicsRow[{
  Plot3D[f[x, y], {x, -4, 4}, {y, -4, 4},
   MeshFunctions -> {#3 &}],
  ContourPlot[f[x, y], {x, -4, 4}, {y, -4, 4},
   Contours -> 15,
   PlotLegends -> Automatic]
  }]

How can I make the Plot3D colors exactly the same as the contour plot colors, shading according to height with the same color strategy as in the contour plot.

$\endgroup$
1
  • 1
    $\begingroup$ You need to learn the difference between = and :=. Your function should be defined as f[x_, y_] := 6 - 3 x - 2 y to protect it from being "destroyed" by assignments to x or y. Recommend you read through this community wiki $\endgroup$
    – m_goldberg
    Oct 5, 2015 at 16:39

1 Answer 1

2
$\begingroup$
f[x_, y_] = 6 - 3 x - 2 y; 
GraphicsRow[{
  Plot3D[f[x, y], {x, -4, 4}, {y, -4, 4}, 
    MeshFunctions -> {#3 &}, ColorFunction -> "DarkRainbow"], 
  ContourPlot[f[x, y], {x, -4, 4}, {y, -4, 4}, Contours -> 15, 
    PlotLegends -> Automatic, ColorFunction -> "DarkRainbow"]}]

enter image description here

$\endgroup$
6
  • 2
    $\begingroup$ I often also explicitly specify the PlotRange->{All,All,{zMin,zMax}} in case I to apply this to different functions for comparison. $\endgroup$
    – N.J.Evans
    Oct 5, 2015 at 15:56
  • $\begingroup$ @N.J.Evans Can you show how you do this with an answer? Thank you. $\endgroup$
    – David
    Oct 5, 2015 at 17:29
  • $\begingroup$ @Algohi Nice answer. But how can I determine the default color function used by ContourPlot. I'd like to use that color in both plots. $\endgroup$
    – David
    Oct 5, 2015 at 17:30
  • 2
    $\begingroup$ You can get the default color function for ContourPlot using ColorFunction->"M10DefaultDensityGradient" in your Plot3D. $\endgroup$
    – N.J.Evans
    Oct 5, 2015 at 18:38
  • $\begingroup$ @N.J.Evans Nice suggestion. Thanks. The standard ContourPlot uses the idea dark color to light color as you go from low to high on the corresponding surface. One of students suggested GrayLevel, which gave a similar comparison. Anyone else have a favorite color that shows low to high in this situation? $\endgroup$
    – David
    Oct 5, 2015 at 21:30

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.