2
$\begingroup$

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}]

enter image description here

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

But the BarLegend is wrong.

How to fix it?

$\endgroup$
2

2 Answers 2

2
$\begingroup$

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}]

Out1


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}]

Out2

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}}, Legending`LegendDump`options$2394]

with Legending`LegendDump`options$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, 
  Charting`TickSide -> Right, ColorFunctionScaling -> False]]

OutPNG

$\endgroup$
1
  • $\begingroup$ 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? $\endgroup$
    – matheorem
    May 25, 2016 at 1:16
0
$\begingroup$

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}]
$\endgroup$
1
  • $\begingroup$ Hi, demm. Thank you for answering. But that is not what I want. I want two plots have the same scale and same BarLegend $\endgroup$
    – matheorem
    May 24, 2016 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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