# BarLegend problem when rescaling color of multiple vectorplot

I want to make multiple vector plot at same color scale with same BarLegend. Similar posts are (1), (2)

I made a minimal example to show the problem I encountered

data = {Table[{y, -x}, {x, -3, 3, 0.2}, {y, -3, 3, 0.2}],
Table[{3 x, 3 y}, {x, -3, 3, 0.2}, {y, -3, 3, 0.2}]};
normData = Map[Norm, data, {3}];
min = Min[normData];
max = Max[normData];

Table[
ListVectorPlot[data[[i]],
PlotRange -> All,
PlotLegends -> BarLegend[{"Rainbow", {min, max}}],
VectorColorFunction ->
Function[{x, y, vx, vy, n},
ColorData["Rainbow"][Rescale[n, {min, max}]]],
VectorColorFunctionScaling -> False, ImageSize -> 300], {i, 1, 2}]


You can see the color scaling of two vectorplot is right.

But the BarLegend is wrong.

How to fix it?

• Related part of the documentation: reference.wolfram.com/language/ref/BarLegend.html#1239470157 Commented May 24, 2016 at 17:09
• @Karsten7. Hi, Karsten7. What do you mean? Set BarLegend to automatic? But that won't work either Commented May 24, 2016 at 17:12

The easiest workaround is to use

Table[Labeled[
ListVectorPlot[data[[i]], PlotRange -> All,
VectorColorFunction ->
Function[{x, y, vx, vy, n},
ColorData["Rainbow"][Rescale[n, {min, max}]]],
VectorColorFunctionScaling -> False, ImageSize -> 300],
BarLegend[{"Rainbow", {min, max}}, ImageSize -> 300],
{{Right, Center}}
], {i, 1, 2}]


This is probably the correct way to specify the BarLegend for the given situation:

Table[ListVectorPlot[data[[i]], PlotRange -> All,
PlotLegends ->
BarLegend[{ColorData["Rainbow"][Rescale[#, {min, max}]] &, {min, max}}],
VectorColorFunction ->
Function[{x, y, vx, vy, n}, ColorData["Rainbow"][Rescale[n, {min, max}]]],
VectorColorFunctionScaling -> False, ImageSize -> 300], {i, 1, 2}]


Alternatively one can add the option ColorFunctionScaling -> True to BarLegend. This option is undocumented for BarLegend and will be highlighted in red in the FE.

Table[ListVectorPlot[data[[i]], PlotRange -> All,
PlotLegends -> BarLegend[{"Rainbow", {min, max}}, ColorFunctionScaling -> True],
VectorColorFunction ->
Function[{x, y, vx, vy, n}, ColorData["Rainbow"][Rescale[n, {min, max}]]],
VectorColorFunctionScaling -> False, ImageSize -> 300], {i, 1, 2}]


### Explanation:

You specified the BarLegend in the format you wanted it to be displayed.
However, as stated in the documentation,

BarLegend automatically picks up values of ColorFunction and ColorFunctionScaling.

For your specific case this means that your BarLegend gets replaced by

BarLegend[{"Rainbow", {0., 12.7279}}, LegendingLegendDumpoptions$2394]  with LegendingLegendDumpoptions$2394 being an internal value that picked up ColorFunctionScaling -> False from the rest of the input (the number at the end will be different each time the input is evaluated) and subsequently BarLegend becomes

BarLegend[{"Rainbow", {0., 12.727922061357857}},
Sequence[LabelStyle -> {}, LegendLayout -> "Column", LegendMarkerSize -> 300,
ChartingTickSide -> Right, ColorFunctionScaling -> False]]


• Thank you so much, Karsten 7. That explained all. The undocumented option for BarLegend is really handy to fix this. How do you know this option? Commented May 25, 2016 at 1:16

Maybe if you consider the min and max of the individual tables within normTable.

min=Map[Min,normData];max=Map[Max,normData];

Table[ListVectorPlot[data[[i]],PlotRange->All,PlotLegends ->BarLegend[{"Rainbow",{min[[i]],max[[i]]}}],VectorColorFunction ->Function[{x,y,vx,vy,n},ColorData["Rainbow"][Rescale[n, {min[[i]], max[[i]]}]]],VectorColorFunctionScaling->False,ImageSize->300],{i,1,2}]
`
• Hi, demm. Thank you for answering. But that is not what I want. I want two plots have the same scale and same BarLegend Commented May 24, 2016 at 16:11