2
$\begingroup$

I am using Mathematica 11.1. It says that Ticks is not an option for BarLegend and I don't know how else to specify the number of labels which appear in a Bar Legend object. All the pages I have visited say that Ticks is an option for Bar Legend. How can I control the number of labels in a Bar Legend? Thanks

$\endgroup$
4
  • $\begingroup$ Use "Ticks". see also the Q/As Problem with custom ticks in BarLegend in Mathematica 11.1 and How to add own description to BarLegend? $\endgroup$
    – kglr
    Commented Jun 30, 2017 at 11:02
  • $\begingroup$ Hi, I have just tried to use "Ticks" but it shows up in Red Highlight saying that it isn't an option for BarLegend? $\endgroup$
    – Duncan
    Commented Jun 30, 2017 at 11:07
  • $\begingroup$ You can do it to some extent with, eg: Table[BarLegend[{"SolarColors", {0, 1}}, i, LegendMarkerSize -> 500], {i, 1, 6}], but it seems to behave oddly. Can you post what you've tried?... Ah, and I'm still using 10.4. So, grain of salt, etc.. $\endgroup$ Commented Jun 30, 2017 at 11:15
  • $\begingroup$ Hi, this is what I've been trying so far: PlotLegends->BarLegend[{"Rainbow",{0.,zmax}},Ticks->Table[i,{i,0.,zmax,∆z}], plotdetail,LegendLabel->Placed[Style[legend,fontsize],Above]]...The Ticks part shows up in Red with or without " ". $\endgroup$
    – Duncan
    Commented Jun 30, 2017 at 11:19

1 Answer 1

8
$\begingroup$

I am not quite sure what do you mean with "specify the number of labels". If you have in mind the number of ticks to the right of the BarLegend it can be done as follows

Row[{
  ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
   ImageSize -> 200, PlotLegends -> BarLegend[Automatic, 3]],


  ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
   ImageSize -> 200, PlotLegends -> BarLegend[Automatic, 6]]
  }]

enter image description here

You may also make a function specifying equally distant ticks

contours = Function[{min, max}, Range[min, max, (max - min)/7]];

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
 ImageSize -> 200, PlotLegends -> BarLegend[Automatic, contours]]

giving

enter image description here

You also may put custom ticks to the BarLegend as in the example below:

    ContourPlot[Sin[x] Cos[y], {x, 0, 2 Pi}, {y, 0, 2 Pi}, 
 Contours -> {Automatic, 10}, ColorFunction -> "Rainbow", 
 PlotLegends -> 
  BarLegend[Automatic, None, "Ticks" -> {-1, 0, 1}, 
   "TickSide" -> Left, "TickLengths" -> 10, 
   "TickLabels" -> (Style[#, 16] & /@ {"cold", "ok", "hot"})]]

enter image description here

Have fun!

$\endgroup$
2
  • 1
    $\begingroup$ Thanks, I did indeed mean the number of ticks. Unfortunately, on my system, it does not recognise "Ticks" as an option for BarLegend. How might I modify this particular line of code to show just 5 ticks without using Automatic? BarLegend[{"Rainbow", zrange}, plotdetail, LegendLabel -> Placed[Style[legend, fontsize], Above]]... Thanks $\endgroup$
    – Duncan
    Commented Jun 30, 2017 at 16:26
  • 1
    $\begingroup$ @Duncan Automatic stays for the ColorFunction and scale. You may specify it according to your liking, such as BarLegend[{"SolarColors", {0, 1}}, 5] which will give you the color type, the scale of the legend from 0 to 1 and 5 ticks. Strange, however, that the key "Ticks" does not work for you. $\endgroup$ Commented Jul 1, 2017 at 12:04

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.