# Customizing DensityPlot[]'s automatic bar legend

To get the density plot of a function f[x,y] with a bar legend, I'm using

DensityPlot[f[x,y],{x,0,2 π},{y,0,2 π},PlotLegends -> Automatic]


I'm using LinTicks[] of SciDraw to have the axes ticks in units of pi. I would also like to customize the ticks in the bar legend to change in units of pi. There are some questions (29643, 55150) here that are trying to solve this problem, but in order to use BarLegend[] one needs to know the range of the function being plotted. For the function I'm plotting, the range is not easy to get analytically. Using a somewhat brute-force approach, I can sample f[x,y] and get the min/max to use with BarLegend[], but I'm looking for a more elegant approach to this. How can I solve this problem? Thanks!

One can use a pure function to specify the ticks of a BarLegend.

f[x_, y_] := Sin[1 + x] + 2 Sin[y] + x

DensityPlot[f[x, y], {x, 0, 2 π}, {y, 0, 2 π},
PlotLegends -> BarLegend[Automatic,
Ticks -> (FindDivisions[{#1, #2, π}, Ceiling[(#2 - #1)/π]] &)]]


• I'm hoping WRI officially addsTick support for BarLegend in 11. (+1). – Edmund Jul 29 '16 at 13:17
• Alternative with minor ticks: Ticks -> ({Range[##, Pi], {Range[##, Pi/4]}} & @@ Round[{#1, #2}, Pi] &) – Karsten 7. Jul 29 '16 at 14:06

You may use Sow to Reap the sample points that DensityPlot uses. Then apply your function to these to get the actual MinMax in the plot. A custom BarLegend can be constructed using FindDivisions and the undocumented ChartingTicks option.

f[x_, y_] = Sin[1 + x] + 2 Sin[y] + x

Module[{dp, eval, range, divs},
{dp, {eval}} =
Reap[DensityPlot[f[x, y], {x, -2, 2}, {y, -3, 3},
ColorFunction -> "Rainbow",
EvaluationMonitor :> Sow[f[x, y]]]];

range = MinMax[eval];

divs = FindDivisions[{Sequence @@ range, {π, π/4}}, {6, 4}];
divs = Join[divs[[1]], {#, ""} & /@ Complement[Flatten@divs[[2]], divs[[1]]]];

Legended[dp,
BarLegend[{"Rainbow", range}, ChartingTicks ->divs]]
]


The downside to this is that the function is evaluated twice at each sample point in DensityPlot which could be an issue depending on how expensive it is to evaluate.

Hope this helps.

# Update

borrowing from @Karsten7. Ticks function syntax. This is much better as you don't have to evaluate twice for each point.

myTicks[min_, max_, multiples_, subdivisions_] :=
Module[{ticks},
ticks = FindDivisions[{min, max, multiples}, subdivisions];
ticks = Join[ticks[[1]], {#, ""} & /@ Complement[Flatten@ticks[[2]], ticks[[1]]]]
]

DensityPlot[f[x, y], {x, 0, 2 π}, {y, 0, 2 π},
ColorFunction -> "Rainbow",
PlotLegends -> BarLegend[Automatic,
Ticks -> (myTicks[#1, #2, {π, π/4}, {6, 4}] &)]]


• I like your use of FindDivisions to generate major and minor ticks, but the whole Join part is unnecessary. The output of FindDivisions already has the right format {list of major ticks, {list of minor ticks}} and the overlap of minor and major ticks is not a problem. See for example the major & minor ticks alternative I added as a comment to my answer. I think subdivisions should be computed automatically or an optional parameter. (+1) – Karsten 7. Jul 29 '16 at 14:20
• @Karsten7. Oh, I didn't realise that. So I can just use Ticks -> (FindDivisions[{#1, #2, {π, π/4}}, {6, 4}] &). Very nice! – Edmund Jul 30 '16 at 18:27
• @Karsten7. Where did you discover the minor-major ticks syntax? I looked at Ticks documentation but did not notice it. Is it an undocumented feature or a documentation bug for Ticks. – Edmund Jul 30 '16 at 18:32