6
$\begingroup$

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!

$\endgroup$

2 Answers 2

6
$\begingroup$

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)/π]] &)]]

Out

$\endgroup$
3
  • 1
    $\begingroup$ I'm hoping WRI officially addsTick support for BarLegend in 11. (+1). $\endgroup$
    – Edmund
    Jul 29, 2016 at 13:17
  • 1
    $\begingroup$ Alternative with minor ticks: Ticks -> ({Range[##, Pi], {Range[##, Pi/4]}} & @@ Round[{#1, #2}, Pi] &) $\endgroup$
    – Karsten7
    Jul 29, 2016 at 14:06
  • $\begingroup$ The syntax highlighter on 11.3 picks up the Ticks as a mistake, albeit it works as intended. Is there a different recommended way to do this? $\endgroup$
    – glS
    May 26, 2020 at 9:08
5
$\begingroup$

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 Charting`Ticks 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}, Charting`Ticks ->divs]]
 ]

enter image description here

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

enter image description here

$\endgroup$
3
  • $\begingroup$ 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) $\endgroup$
    – Karsten7
    Jul 29, 2016 at 14:20
  • $\begingroup$ @Karsten7. Oh, I didn't realise that. So I can just use Ticks -> (FindDivisions[{#1, #2, {π, π/4}}, {6, 4}] &). Very nice! $\endgroup$
    – Edmund
    Jul 30, 2016 at 18:27
  • $\begingroup$ @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. $\endgroup$
    – Edmund
    Jul 30, 2016 at 18:32

Your Answer

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

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