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'm sensing this might be a rookie mistake but normally I roll my own legends but decided to try the built in legends for a ListPlot3D. PlotLegends is listed as an option for ListPlot3D but curiously in V9 there are no usage examples. On the other hand there are several examples for Plot3D. Specifically I was trying to create a legend analogous to this example from the Plot3D docs.

ListPlot3D[Table[Sin[x y], {y, 0, 3, 0.1}, {x, 0, 3, 0.1}], 
 ColorFunction -> "Rainbow", PlotLegends -> Automatic]

enter image description here

However I want to use my own specific ColorFunction but it appears that as soon as you depart from the named built-in string colour functions PlotLegends ceases to work.

I know how to "roll my own" so I am not seeking assistance with creating my own function. I simply want to know if it is possible to make something similar using the built in options for ListPlot3D, whether I am overlooking something or whether this is a bug.

Edit

From Wolfram: the current design of the PlotLegends -> Automatic option is to turn off the plot legend when a pure function (as ColorFunction) is present.

which means that this

With[{cf = Blend[{Blue, White, Red}, #] &,
data = Table[Sin[x y], {y, 0, 3, 0.1}, {x, 0, 3, 0.1}]},
    ListPlot3D[data, ColorFunction -> cf, 
        PlotLegends -> Automatic
    ]
]

doesn't work ...which would appear to make it a design oversight rather than a bug.

share|improve this question
    
Feels very bug-like to me ... can't really see why it couldn't work, especially after looking at rm's example. EDIT: Works for the completely analogous DensityPlot, but not for Plot3D. I'd say that settles it's a bug. –  Szabolcs Feb 14 at 3:12
    
@Szabolcs the answer from rm -rf works but it does seem like a bug that Automatic doesn't work for a valid user ColorFunction –  Mike Honeychurch Feb 14 at 3:35
add comment

1 Answer

up vote 10 down vote accepted

If you have your own color function, then passing an explicit BarLegend to PlotLegends seems to be the only way out. This also means that you'll have to feed the min/max for your data, but this isn't hard. Here's an example:

With[{cf = Blend[{Blue, White, Red}, #] &, data = Table[Sin[x y], {y, 0, 3, 0.1}, {x, 0, 3, 0.1}]},
    ListPlot3D[data, ColorFunction -> cf, 
        PlotLegends -> BarLegend[{cf, Through[{Min, Max}@data]}]
    ]
]

share|improve this answer
    
This works. Shame the Automatic option doesn't work given that it does for the built in ColorFunctions. –  Mike Honeychurch Feb 14 at 3:34
add comment

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.