1
$\begingroup$

I am trying to plot in 3D and I need to use PlotLegend. My code is

Needs["PlotLegends`"]
Evaluate[PlotLegends]
Plot3D[Tanh[H/T], {T, 0, 300}, {H, 0.001, 1}, 
PlotLabel -> 
  Style[Framed[
    "Energy Dependance on Magnetic Field H and Absolute Temperature \
T"], 13, Red, Background -> Lighter[LightYellow]], 
 Background -> LightGray, 
 ColorFunction -> Function[{x, y, z}, Hue[.65 (1 - z)]], 
 AxesLabel -> {T[kelvin], H[tesla]}, BoundaryStyle -> Thick, 
 ClippingStyle -> Opacity[0.5], Mesh -> Full, 
 PlotLegends -> Automatic]

While I use Needs["PlotLegends`"] I don't seem to be able to plot a legend. I get an error saying

Plot3D::optx: Unknown option PlotLegends in Plot3D[...]

How can this be fixed?

$\endgroup$
1
  • 2
    $\begingroup$ This is an example from the documentation on how to use PlotLegend: ShowLegend[ Plot3D[Sin[x y], {x, 0, \[Pi]}, {y, 0, \[Pi]}, ColorFunction -> "Rainbow"], {ColorData["Rainbow"][1 - #1] &, 10, " 1", "-1", LegendPosition -> {1.1, -0.4}}] $\endgroup$
    – VLC
    Dec 19, 2012 at 17:28

1 Answer 1

2
$\begingroup$

I returned to the problem after reading the comments above. In version 8, the PlotLegends -> option works for Plot[] but not for Plot3D[]. For Plot3D, you will have to use ShowLegend[], as demonstrated below:

ShowLegend[
 Plot3D[Tanh[H/T], {T, 0, 300}, {H, 0.001, 1}, 
  PlotLabel -> 
   Style[Framed[
     "Energy Dependance on Magnetic Field H and Absolute Temperature \
T"], 13, Red, Background -> Lighter[LightYellow]],(*Background->
  LightGray,*)ColorFunction -> Function[{x, y, z}, Hue[.65 (1 - z)]], 
  AxesLabel -> {T[kelvin], H[tesla]}, BoundaryStyle -> Thick, 
  ClippingStyle -> Opacity[0.5], 
  Mesh -> Full], {{{Graphics[{White, Circle[{0, 0}, 0]}], Tanh[H/T]}},
   LegendPosition -> {1.1, -.4}}]

This becomes simpler in version 9, where you can use PlotLegends-> if you like

Plot3D[Tanh[H/T], {T, 0, 300}, {H, 0.001, 1}, 
 PlotLabel -> 
  Style[Framed[
    "Energy Dependance on Magnetic Field H and Absolute Temperature \
T"], 13, Red, 
   Background -> Lighter[LightYellow]],(*Background->LightGray,*)
 ColorFunction -> Function[{x, y, z}, Hue[.65 (1 - z)]], 
 AxesLabel -> {T[kelvin], H[tesla]}, BoundaryStyle -> Thick, 
 ClippingStyle -> Opacity[0.5], Mesh -> Full, 
 PlotLegends -> 
  SwatchLegend["Expressions"]]
$\endgroup$
4
  • $\begingroup$ I tried your solution but it is still not working. I keep getting the error message that Plot3D::optx: Unknown option PlotLegends in Plot3D[...] $\endgroup$
    – Thanos
    Dec 19, 2012 at 19:17
  • $\begingroup$ Try the revised code above. $\endgroup$ Dec 19, 2012 at 22:20
  • $\begingroup$ It's working!!! Thank you very much!!! However I noticed something weird... I cannot rotate the plot...Is this suppossed to happen? $\endgroup$
    – Thanos
    Dec 20, 2012 at 17:14
  • $\begingroup$ Do you mean click-and-drag rotation, or manually specifying the angle in the Plot3D command? p.s. do a brother a favor and accept my answer above? $\endgroup$ Dec 21, 2012 at 19:08

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.