8
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented Feb 14, 2014 at 3:12
  • $\begingroup$ @Szabolcs the answer from rm -rf works but it does seem like a bug that Automatic doesn't work for a valid user ColorFunction $\endgroup$ Commented Feb 14, 2014 at 3:35

1 Answer 1

12
$\begingroup$

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]}]
    ]
]

$\endgroup$
1
  • $\begingroup$ This works. Shame the Automatic option doesn't work given that it does for the built in ColorFunctions. $\endgroup$ Commented Feb 14, 2014 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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