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?

    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.

  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
    {Hue, {-Pi, Pi}},
    Ticks -> Table[i, {i, -Pi, Pi, Pi/4}],
    LegendMarkerSize -> 300

Plot Ticks

  • $\begingroup$ It seems Ticks is not an option for BarLegend, or at least in Mathematica 12.1. $\endgroup$ – AmirHosein Sadeghimanesh Jan 17 at 9:38

I wonder whether you would be content with a simpler SwatchLegend:

 Table[{2 Pi Sin[x], 2 Pi Cos[x]}, {x, 0, 2 Pi}],
 ColorFunction -> "BlueGreenYellow",
 ImageSize -> 500,
 PlotLegends -> 
    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[#]] &)]]


  • $\begingroup$ 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. $\endgroup$ – Ceren Burcak Aug 2 '13 at 15:13
  • $\begingroup$ @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... $\endgroup$ – 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}];
  DataRange -> {{0, 10}, {0, 1.2}},
  FrameLabel -> {"x", "y"},
  ColorFunction -> Hue,
  ImageSize -> 250]

enter image description here

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, 

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

 {ListDensityPlot[array, DataRange -> {{0, 10}, {0, 1.2}},
   ColorFunction -> (ColorData["Pastel"])],
    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]

enter image description here

  • $\begingroup$ What is Draw supposed to be? The code generates an error because it's not a graphics primitive. $\endgroup$ – C. E. Aug 1 '13 at 23:25
  • 1
    $\begingroup$ 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.) $\endgroup$ – David Park Aug 1 '13 at 23:28
  • $\begingroup$ 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? $\endgroup$ – gpap Aug 2 '13 at 14:31
  • $\begingroup$ 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. $\endgroup$ – Ceren Burcak Aug 2 '13 at 15:10
  • $\begingroup$ By the way, thanks for the information on the listdensityplot function. $\endgroup$ – 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,

 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.

enter image description here


  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.

enter image description here

An example snapshot follows.

  • $\begingroup$ In my experiments, i found that the legend always ended up with de-rationalized numbers... $\endgroup$ – cormullion Aug 2 '13 at 13:42
  • $\begingroup$ I don't see you using LabelStyle, could you elaborate on it not working, please? $\endgroup$ – rcollyer Aug 2 '13 at 20:18
  • $\begingroup$ 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. $\endgroup$ – ubpdqn Aug 2 '13 at 21:28
  • $\begingroup$ That's why I'm asking, see my profile, and I'd like to know what value you gave to LabelStyle. $\endgroup$ – rcollyer Aug 3 '13 at 0:45
  • $\begingroup$ 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. $\endgroup$ – ubpdqn Aug 3 '13 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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