4
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ Can confirm this happens in 10.3 Win 7 Pro 64 bit. $\endgroup$ – Edmund Jan 27 '16 at 16:52
  • 1
    $\begingroup$ @Edmund not a bug, user error. $\endgroup$ – rcollyer Jan 27 '16 at 17:02
  • $\begingroup$ 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. $\endgroup$ – becko Jan 27 '16 at 17:14
  • $\begingroup$ @becko not mine. I think it is a good question. I just upvoted it. $\endgroup$ – rcollyer Jan 27 '16 at 17:15
  • $\begingroup$ 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. $\endgroup$ – Edmund Jan 27 '16 at 17:17
4
$\begingroup$

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.

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

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ To the downvoter, if this were a bug this would be a good way of working around the problem, and was my solution. $\endgroup$ – rcollyer Jan 27 '16 at 17:05
  • $\begingroup$ 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. $\endgroup$ – Edmund Jan 27 '16 at 17:17
  • $\begingroup$ @Edmund answered above. $\endgroup$ – rcollyer Jan 27 '16 at 17:22
3
$\begingroup$

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.

| improve this answer | |
$\endgroup$

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.