10

Is there a default-option possibility to add own defined names of the contour-labels in BarLegend? I want to have a string "hot" for 1 and "cold" for -1 for this:

ContourPlot[Sin[x] Cos[y], {x, 0, 2 Pi}, {y, 0, 2 Pi}, 
 Contours -> {Automatic, 10}, ColorFunction -> "Rainbow", 
 PlotLegends -> BarLegend[Automatic, None]]

How can I do this without writing a separate function?

kglr
  • 394,356
  • 18
  • 477
  • 896
szogun
  • 141
  • 6
  • 1
    I believe this should solve the problem. PlotLegends -> BarLegend[Automatic, Method -> {Ticks -> {{-1, "Cold"}, {0, "OK"}, {1, "Hot"}}}] – Szabolcs Mar 01 '15 at 21:44
  • @Szabolcs you can drop the Method, putting in Ticks -> ... directly seems to work. – rcollyer Sep 07 '16 at 03:17

1 Answers1

18

Update 3: The issue mentioned in update 2 below is fixed in version 12.0.

Update 2: In version 11.1, the option setting "Ticks" -> {{-1, "cold"}, {0, "ok"}, {1, "hot"}} doesn't work (See Problem with custom ticks in BarLegend in Mathematica 11.1). The tick labels are sorted:

 ContourPlot[Sin[x] Cos[y], {x, 0, 2 Pi}, {y, 0, 2 Pi}, 
  Contours -> {Automatic, 10}, ColorFunction -> "Rainbow", 
  PlotLegends -> BarLegend[Automatic, None, 
   "Ticks" -> {{-1, "cold"}, {0, "ok"}, {1, "hot"}}]]

Mathematica graphics

To get the correct labeling of ticks, we can use the option "TickLabels":

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}, "TickLabels" -> {"cold", "ok", "hot"}]]

Mathematica graphics

Original post:

You can use the (afaik undocumented) option "Ticks"

ContourPlot[Sin[x] Cos[y], {x, 0, 2 Pi}, {y, 0, 2 Pi}, 
 Contours -> {Automatic, 10}, ColorFunction -> "Rainbow", 
 PlotLegends -> BarLegend[Automatic, None, "Ticks" -> {{-1, "cold"}, {0, "ok"}, {1, "hot"}}]]

enter image description here

Note: You can also use Ticks instead of "Ticks" and ignore the red highlighting that suggest syntax error.

Update 1: Using additional BarLegend options "TickLabels", "TickSide", "TickLengths":

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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • I believe you can use any option inside Method, like this. – Szabolcs Mar 01 '15 at 21:45
  • @Szabolcs, nice. In version 9 (Windows 8 x 64) we need to use FrameStyle with PlotRangePadding, i.e., Method->{FrameStyle->Thick, PlotRangePadding -> 0}. Without PlotRangePadding->0 we get what looks like two pairs of horizontal frames. – kglr Mar 01 '15 at 22:11
  • Thanks, it solves my problem. By the way, it is strange that the option Ticks allowed in other functions highlights in red for this one, but the result is as one can expect. Isn't it an another bug? – szogun Mar 02 '15 at 18:26
  • @szogun, you are welcome. Ticks is not a documented option for BarLegend; so I don't think we can call it a bug no matter how it behaves/misbehaves:) – kglr Mar 02 '15 at 18:34
  • Running mathematica 11.1.1, the first code listing permutes the positions of "hot" "cold", and "ok" so that "ok" is at the top and "hot" is in the middle. The second code listing still gives the correct output. – Brian Moths May 12 '17 at 02:24
  • Thank you @NowIGetToLearnWhatAHeadIs. Strange, isn't it? – kglr May 12 '17 at 02:29
  • @kglr Ya I experimented a little and I think it sorts the labels in alphabetical order with the beginning of the alphabet at the bottom of the barlegend. – Brian Moths May 12 '17 at 02:58
  • It doesnt work when you change the color set: BarLegend["SunsetColors" .....} just doisplays two ticks, not three, namely "hot", "ok" – Joshua Salazar May 03 '23 at 01:02