1
$\begingroup$

I've been trying to get BarLegend to work with Mathematica's Plot3D coloring example function. I wanted to use coloring to indicate the height of the function, and the Plot3D documentation provides this as an example:

Plot3D[x/Exp[x^2 + y^2], {x, -2, 2}, {y, -2, 2}, ColorFunction -> Function[{x, y, z}, Hue[.65 (1 - z)]]] 

I have my own function and domain, but I copied Mathematica's coloring function directly:

Plot3D[{3.5 Log[1.7*x^3 + 0.8*Sin[y] +3]}, {x, 1, 100}, {y, 10^-5, 1}, ColorFunction -> Function[{x, y, z}, Hue[.65 (1 - z)]]] 

The result is: enter image description here

The problem occurs when I try to create a legend for the coloring. None of the following works:

BarLegend[ColorFunction -> Function[{x, y, z}, Hue[.65 (1 - z)]]], BarLegend[Function[{x, y, z}, Hue[.65 (1 - z)]]], BarLegend[Hue[.65 (1 - z)]] 

I did a bit of reading on this site, and the best I could find was the following. I can manually find the minimum and maximal values that the function takes on my domain of interest:

minmax = {5.41647, 50.2115}; BarLegend[{Function[x, Reverse@Hue[0.65 Rescale[x, minmax]]], minmax}] 

enter image description here (sorry the resizing for this image isn't working)

This gets a bit closer to what I want, but the legend is flipped upside down (I am also not certain that the ticks/numbers are exactly correct).

Would you know how to fix this/get the right legend to appear?

Thank you!

$\endgroup$

1 Answer 1

1
$\begingroup$
cf1[x_] := Hue[2/3 x] cf2[x_] := Hue[2/3 - 2/3 x] Plot3D[{3.5 Log[1.7*x^3 + 0.8*Sin[y] + 3]}, {x, 1, 100}, {y, 10^-5, 1}, ColorFunction -> cf1, PlotLegends -> Automatic] Plot3D[{3.5 Log[1.7*x^3 + 0.8*Sin[y] + 3]}, {x, 1, 100}, {y, 10^-5, 1}, ColorFunction -> cf2, PlotLegends -> Automatic] Plot3D[{3.5 Log[1.7*x^3 + 0.8*Sin[y] + 3]}, {x, 1, 100}, {y, 10^-5, 1}, ColorFunction -> "Rainbow", PlotLegends -> Automatic] 

enter image description here

enter image description here

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks so much! It looks like you were able to generate the legend by adding "PlotLegends -> Automatic." If I want to specify the ticks on the plot and their fontsize (e.g. I was using "Ticks -> {{10, 25, 50}, {0.00001, 0.1, 0.5, 0.75}, {10, 20, 30, 40}}, AxesLabel -> {"X-axis", "Y-axis", "Function Value"}, LabelStyle -> 14"), adding in the PlotLegends messes up the axes label placement in the figure it generates. Is there a way to get around this? However, this is not a big deal since I can just create 2 separate figures and paste the legend onto the axes-labeled one if necessary. $\endgroup$ Commented Oct 7 at 20:16
  • $\begingroup$ @QuaBrTheorist - perhaps you want the legend below the plot, e..g., PlotLegends -> Placed[BarLegend[{Hue[.65 (1 - #)] &, {min, max}}], Below] $\endgroup$ Commented Oct 7 at 20:57
  • $\begingroup$ Unfortunately, the placement is still off (the labels overlap the figure), especially when I move the figure around to view it from a different angle. But that's ok - for my purposes, I can just create 2 separate figures, one with and one without the legend. $\endgroup$ Commented Oct 9 at 22:46
  • $\begingroup$ @QuaBrTheorist There is also AxesEdge -> {{-1, -1}, {-1, 1}, {-1, -1}}, choose appropriate mix of 1 and -1, but probably also needs to be adjusted for each different view point. $\endgroup$ Commented Oct 10 at 8:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.