Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
2  
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}}] –  VLC Dec 19 '12 at 17:28

1 Answer 1

up vote 1 down vote accepted

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"]]
share|improve this answer
    
I tried your solution but it is still not working. I keep getting the error message that Plot3D::optx: Unknown option PlotLegends in Plot3D[...] –  Thanos Dec 19 '12 at 19:17
    
Try the revised code above. –  Michael Stern Dec 19 '12 at 22:20
    
It's working!!! Thank you very much!!! However I noticed something weird... I cannot rotate the plot...Is this suppossed to happen? –  Thanos Dec 20 '12 at 17:14
    
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? –  Michael Stern Dec 21 '12 at 19:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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