Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Execute this code:

ArrayPlot[RandomReal[{-1, 1}, {10, 10}],
 PlotLegends -> BarLegend[{"BrownCyanTones", {-5, 1}}],
 ColorFunction -> ColorData[{"BrownCyanTones", {-5, 1}}],
 PlotRange -> {-5, 1},
 ColorFunctionScaling -> False]

In my PC (Xubuntu 15.04, Mathematica 10.3) this returns:

enter image description here

You can see that the BarLegend is wrong, it should be:

enter image description here

which is what I get if I had executed BarLegend[{"BrownCyanTones", {-5, 1}}] directly.

Is this a bug? How can I fix it?

share|improve this question
1  
Can confirm this happens in 10.3 Win 7 Pro 64 bit. – Edmund Jan 27 at 16:52
1  
@Edmund not a bug, user error. – rcollyer Jan 27 at 17:02
    
Why the close vote? It may not be a bug, but the proper way to do this is not in the documentation. So this is a valid question. – becko Jan 27 at 17:14
    
@becko not mine. I think it is a good question. I just upvoted it. – rcollyer Jan 27 at 17:15
    
I would disagree that it is not an error. The documentation clearly states that a legend constructor can be used for PlotLegends and even explictly includes BarLegend in in the list. The OP has used a valid option and a valid expression for the option and has not gotten the expected result. – Edmund Jan 27 at 17:17
up vote 3 down vote accepted

You are using it wrong. The default algorithm for PlotLegends will supply the correct ColorFunction to BarLegend, so use

ArrayPlot[RandomReal[{-1, 1}, {10, 10}], 
  PlotLegends -> Automatic, 
  ColorFunction -> ColorData[{"BrownCyanTones", {-5, 1}}], 
  PlotRange -> {-5, 1}, ColorFunctionScaling -> False]

enter image description here

If you need to modify the legend, you can do so without mishap by using Automatic, e.g.

PlotLegends -> BarLegend[Automatic, LegendLayout -> "Row"]
PlotLegends -> BarLegend[{Automatic, {-10, 10}}]
PlotLegends -> BarLegend[Automatic, 5 (*contours*)]

etc.

share|improve this answer
    
This should be documented. It is not obvious that this is the right way to do it. – becko Jan 27 at 17:14
2  
@becko It is in the details section of ref/BarLegend. As always, there could be more examples of it ... – rcollyer Jan 27 at 17:18

Can confirm this happens in 10.3 Win 7 Pro 64 bit.

You can get around it with Legended.

Legended[
 ArrayPlot[RandomReal[{-1, 1}, {10, 10}], 
  ColorFunction -> ColorData[{"BrownCyanTones", {-5, 1}}], 
  PlotRange -> {-5, 1}, ColorFunctionScaling -> False],
 BarLegend[{"BrownCyanTones", {-5, 1}}]]

enter image description here

Hope this helps.

share|improve this answer
    
To the downvoter, if this were a bug this would be a good way of working around the problem, and was my solution. – rcollyer Jan 27 at 17:05
    
@rcollyer Agree (+1). I was about to accept this answer first. – becko Jan 27 at 17:15
    
I would disagree that it is not an error. The documentation clearly states that a legend constructor can be used for PlotLegends and even explictly includes BarLegend in in the list. – Edmund Jan 27 at 17:17
    
@Edmund answered above. – rcollyer Jan 27 at 17:22

To my mind there are four ways of making the array plot with the BrownCyanTones gradient and a bar legend that seem reasonable.

Data range -1 to 1, plot range -1 to 1, lengend range -1 to 1

SeedRandom[1];
ArrayPlot[RandomReal[{-1, 1}, {10, 10}],
  ColorFunction -> "BrownCyanTones",
  PlotLegends -> BarLegend[{"BrownCyanTones", {-1, 1}}]]

plot1

Data range -5 to 1, plot range -5 to 1, lengend range -5 to 1

SeedRandom[1];
ArrayPlot[RandomReal[{-5, 1}, {10, 10}],
  ColorFunction -> "BrownCyanTones",
  PlotLegends -> BarLegend[{"BrownCyanTones", {-5, 1}}]]

plot2

Data range -1 to 1, plot range -1 to 1, legend colors taken from the upper part of BrownCyanTones

SeedRandom[1];
ArrayPlot[RandomReal[{-1, 1}, {10, 10}], 
  ColorFunction -> ColorData[{"BrownCyanTones", {-1, 1}}],
  PlotLegends -> BarLegend[{ColorData[{"BrownCyanTones", {-1, 1}}], {-1, 1}}]]

plot3

Data range -1 to 1, plot range -5 to 1, lengend range -5 to 1

SeedRandom[1];
ArrayPlot[RandomReal[{-1, 1}, {10, 10}], 
  ColorFunction -> ColorData[{"BrownCyanTones", {-1, 1}}],
  PlotRange -> {-5, 1},
  PlotLegends -> BarLegend[{"BrownCyanTones", {-5, 1}}]]

plot4

I can't figure out from your question which of these is what you are after, but one of them should work. They all have the property that the bar legend colors match the plot colors.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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