3
$\begingroup$

I tried

Plot3D[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2},
 ColorFunction -> (ColorData["TemperatureMap"][#3] &),
 PlotLegends -> Automatic]

in an attempt to add a colorbar legend to the plot, but this did not work. It did work here:

DensityPlot[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2},
 ColorFunction -> "SunsetColors",
 PlotLegends -> Automatic]

Any suggestions in how I can get it to work in the first bit of code?

Thanks.

$\endgroup$
1
  • 1
    $\begingroup$ Try specifying it manually, e.g. PlotLegends -> BarLegend["TemperatureMap"]. You'd need additional customizations in most cases to make sure the legend matches the plot. $\endgroup$
    – Szabolcs
    Mar 15, 2013 at 19:00

1 Answer 1

3
$\begingroup$

A little twisted, and needs some generalisation:

<< PlotLegends`;
c = ColorData["TemperatureMap"];
s := Plot3D[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2},  ColorFunction -> (c[#3] &), 
            ImageSize -> {300, 300}];
k = ShowLegend[ s /. Plot -> DensityPlot, {c[1 - #1] &, 10, " 1", " 0",  
              LegendPosition -> {1.1, -.4}, LegendShadow -> None}] /.  
              Graphics[{_, b_}, ___] :> Graphics[b, ImageSize -> 50];
GraphicsGrid[{{s, k}}]

Mathematica graphics

$\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.