Changing the tick labels in a BarLegend

In the following plot, the colors mean the phase, so I need to label the color bar not in integers, but in increments of $\pi$. I searched, but could not find how to change the ticks. Does anyone have an idea?

ListDensityPlot[
Table[ArcTan[Cos[x + y], Sin[x + y]], {x, 0, 10, 0.01}, {y, 0, 1.2, 0.01}],
DataRange -> {{0, 10}, {0, 1.2}}, PlotLegends -> Automatic, ColorFunction -> Hue] I consider myself an amateur. I was also interested in this question, and I found the following code solves the problem. However, the command that solves the problem ("Ticks") shows up highlighted in red. You can change many of the features of the legend, like size, add a label, etc.

Legended[
ListDensityPlot[Table[ArcTan[Cos[x + y], Sin[x + y]],
{x, 0, 10, 0.01}, {y, 0, 1.2, 0.01}],
DataRange -> {{0, 10}, {0, 1.2}},
ColorFunction -> Hue
],
BarLegend[
{Hue, {-Pi, Pi}},
Ticks -> Table[i, {i, -Pi, Pi, Pi/4}],
LegendMarkerSize -> 300
]
]  I wonder whether you would be content with a simpler SwatchLegend:

ListDensityPlot[
Table[{2 Pi Sin[x], 2 Pi Cos[x]}, {x, 0, 2 Pi}],
ColorFunction -> "BlueGreenYellow",
ImageSize -> 500,
PlotLegends ->
SwatchLegend[
Table[
Blend[{Blue, Green, Yellow}, Rescale[x, {0, 2 Pi}]],
{x, 0, 2 Pi, Pi/8}],
Range[0, 2 Pi, Pi/8],
LegendMarkers -> Graphics[Rectangle[]],
LegendMarkerSize -> 15,
LegendLayout -> (Grid[Reverse[#]] &)]] • Thanks! A bit closer to what I want, however I am still curious if it is really possible to label the a bar legend with the increments of Pi. – Ceren Burcak Aug 2 '13 at 15:13
• @ceren I'm interested to know whether it's possible -I couldn't manage it but perhaps there is a more devious way waiting to be discovered... – cormullion Aug 2 '13 at 16:00

First, I'm not certain if your plot is correct. Notice that ListDensityPlot arranges successive rows of the array up the page, and successive columns across. The first iterator in the table creates the rows and the second iterator creates the columns. So if you want the normal x and y axis you have to reverse the iterators. On the other hand, DataRange coincides with what we normally call the x and y axis. The WRI examples are poor because they seem to all be symmetrical and thus don't illustrate this important issue. Here is a case where all the variation occurs in the x direction.

arrayTest = Table[x/10, {y, 0, 1.2, 0.01}, {x, 1, 10, 0.01}];
ListDensityPlot[arrayTest,
DataRange -> {{0, 10}, {0, 1.2}},
FrameLabel -> {"x", "y"},
ColorFunction -> Hue,
ImageSize -> 250] I think that is what is intended. If I'm wrong about this please correct me.

In the sample plot, I would say: why have a legend at all? Why not just draw the constant ArcTan values on the plot and label them. In the following I use Tooltips, and also special labeling along the top x axis. (It would have been easier to reverse the top and bottom.) I also used pastel colors so the constant ArcTan lines would better show. Here is the plot using Show. (I have corrected the following so that it is entirely in regular Mathematica.)

array = Table[
ArcTan[Cos[x + y], Sin[x + y]], {y, 0, 1.2, 0.01}, {x, 0, 10,
0.01}];

xticks = MapThread[{#1, #2} &, {Table[
1/5 (-6 + 5 k \[Pi]), {k, 1, 3}], {-\[Pi], 0, \[Pi]}}];

Show[
{ListDensityPlot[array, DataRange -> {{0, 10}, {0, 1.2}},
ColorFunction -> (ColorData["Pastel"])],
Table[Plot[
Tooltip[ \[Pi] k - x , Mod[k \[Pi], 2 \[Pi], -\[Pi]]], {x, 0, 10},
PlotRange -> {0, 1.2}], {k, 0, 3, 1/2}]},
FrameLabel -> {"x", "y"},
FrameTicks -> {{Automatic, Automatic}, {Automatic, xticks}},
ImageSize -> 250] • What is Draw supposed to be? The code generates an error because it's not a graphics primitive. – C. E. Aug 1 '13 at 23:25
• Sorry, I made a mistake. I should have used Plot and taken it out of Graphics. (I developed it with Presentations and then converted to normal Mathematica and forgot about the Draw.) – David Park Aug 1 '13 at 23:28
• As per your comment re ListDensityPlot and the way the matrices are plotted I agree that it's an important issue and also very frustrating. At the same time it's not consistent with functions like ArrayPlot that have the expected behaviour. Have you got any idea why this is so? – gpap Aug 2 '13 at 14:31
• Thanks for the answer, but what I am actually working on is not this function at all. This is only an example code to tell about my problem. Since the color distribution is quite random in my original research, labelling them on the axis with the increments of Pi will not look explanatory, I assume. That is why I need a color bar which shows ticks in increments of Pi. – Ceren Burcak Aug 2 '13 at 15:10
• By the way, thanks for the information on the listdensityplot function. – Ceren Burcak Aug 2 '13 at 15:17

http://reference.wolfram.com/mathematica/ref/Files/BarLegend.en/I_70.gif The code in the link as well as documentation of BarLegend may be helpful in customising bar legend. Look at Contours option.

I was overly optimistic, however,

ListDensityPlot[
Table[ArcTan[Cos[x + y], Sin[x + y]], {x, 0, 10, 0.01}, {y, 0, 1.2,
0.01}], DataRange -> {{0, 10}, {0, 1.2}},
PlotLegends ->
Placed[BarLegend[Automatic, {-2, -1, -0.5, 0, 0.5, 1, 2},
LegendMarkerSize -> 250,
LegendFunction -> (Framed[#, RoundingRadius -> 5] &),
LegendLabel -> "z-value"], {After, Top}], ColorFunction -> Hue]

is partially successful. The ticks for -1, 1 are not displayed. The LabelStyle option does not work but perhaps this is a start. EDIT

Manipulate[
ListDensityPlot[
Table[ArcTan[Cos[x + y], Sin[x + y]], {x, 0, 10, 0.01}, {y, 0, 1.2,
0.01}], DataRange -> {{0, 10}, {0, 1.2}},
PlotLegends ->
Placed[BarLegend[Automatic, p, LegendMarkerSize -> 250], Bottom],
ColorFunction ->
Hue], {p, {{-2, -1, -0.5, 0, 0.5, 1, 2}, {-Pi/2, Pi/2}, {-1, 0,
1}, {-0.3, 0, 0.7}}}]

The above runs slowly on my old machine. However, illustrates the capacity to change tick labels with the caveat that sometimes not all specified contours are given a tick label. Further Dynamic@p does not work as a way to speed up processing. Note {-Pi/2,Pi/2} is presented at 3 decimal places. An example snapshot follows.

• In my experiments, i found that the legend always ended up with de-rationalized numbers... – cormullion Aug 2 '13 at 13:42
• I don't see you using LabelStyle, could you elaborate on it not working, please? – rcollyer Aug 2 '13 at 20:18
• In BarLegend documentation LabelStyle appears as an option. However, it is not recognised when put in the code above. So I did not put it in. I am merely stating an observation that you can test. I do not know why it does not work. – ubpdqn Aug 2 '13 at 21:28
• That's why I'm asking, see my profile, and I'd like to know what value you gave to LabelStyle. – rcollyer Aug 3 '13 at 0:45
• I apologise if you interpreted my reply as disrespectful. I am a novice. When I placed LabelStyle in the position after Contours it was highlighted Red. I ran the code anyway using {FontSize->20}. The error message related to option LabelStyle not recognized. I checked for package dependencies and there seemed to be none. However, I am extremely happy to learn. – ubpdqn Aug 3 '13 at 0:53