# Issues with ColorFunction Scaling when using RegionFunction in VectorPlot

I was plotting a VectorPlot and restricting it to the physically meaningful region using RegionFunction. To my surprise, the result for the following code was an empty plot.

tempa = 0.2;
vectorfield = {(0.02*x y)/(x^2 + y^2)^(5/2),
1/(x^2 + y^2) (x^2 (1 - 0.008/(x^2 + y^2)^(3/2)) + y^2 (1 + 0.016/(x^2 + y^2)^(3/2)))}
VectorPlot[vectorfield, {x, -1, 1}, {y, -1, 1},
RegionFunction -> ((tempa^2 < (#1^2 + #2^2 ) < 1 && #2 < 0) &),
VectorColorFunction -> "Rainbow"]


Investigating the problem further by removing the RegionFunction, I realized that the probem was with the vector's value at the origin, which is very large. In fact, the following code with the RegionFunction option removed returns the following image.

VectorPlot[vectorfield, {x, -1, 1}, {y, -1, 1},
VectorColorFunction -> "Rainbow"]


By adding the options of VectorPoints and VectorScale, I was able to display some meaningful information, as shown below.

 VectorPlot[vectorfield, {x, -1, 1}, {y, -1, 1},
VectorPoints -> {20, 20}, VectorScale -> 0.5,
RegionFunction -> ((tempa^2 < (#1^2 + #2^2 ) < 1 && #2 < 0) &),
VectorColorFunction -> "Rainbow"]


Alternatively, using VectorScale as follows

 VectorPlot[vectorfield, {x, -1, 1}, {y, -1, 1},
VectorPoints -> {20, 20}, VectorScale -> {Automatic, Automatic, If[#5 > 3, 0, #5] &},
RegionFunction -> ((tempa^2 < (#1^2 + #2^2 ) < 1 && #2 < 0) &),
VectorColorFunction -> "Rainbow"]


seems to be a better way to scale the length of the vectors, but it still doesn't help with the ColorFunction.

I would expect Mathematica to scale the colors of the vectors based on the range of norms in the RegionFunction. Is this not the case, or is there something wrong with my code?

-
Adding VectorScale -> {Automatic, Automatic, If[#5 > 3, 0, #5] &} as an option seems to help with the scaling for the initial length of the vectors but doesn't help with the color function - perhaps I'm doing something wrong about the color. –  Vincent Tjeng Apr 16 '13 at 5:20
What happens if you crank up VectorPoints? –  Guess who it is. Apr 16 '13 at 5:52
On my system (I use Mathematica 9 on Windows), when I crank up the points (say to {30, 30} while keeping VectorScale at 0.5, then the points get smaller and smaller for constant image size. It doesn't seem to help with the colour, either. –  Vincent Tjeng Apr 16 '13 at 5:59
With respect to the color, it would seem that the norms do not vary too much, and that would explain why the colors are uniform (did you notice that your vectors in your last picture have very nearly the same lengths?) Maybe change your VectorColorFunction to color according to some other criterion. –  Guess who it is. Apr 16 '13 at 6:46
@J.M. my understanding is that Mathematica normally scales the colors such that they range over the full palette of colors, no matter what the absolute value of the norm is. For instance, VectorPlot3D[verysmallnumber*{x, y, z}, {x, -1, 1}, {y, -1, 1}, {z, -1, 1}, VectorColorFunction -> "Rainbow"] gives the same image no matter what value you choose for verysmallnumber as the values are normalized. –  Vincent Tjeng Apr 16 '13 at 6:51

There is a VectorColorFunctionScaling option to VectorPlot that seems to fix the problem. @JM is right, though, as it is now, you are amplifying the scale of your vectors so that they look nicer in the plot but their actual norms vary very little. In any case, if you try the following (Hue has more vibrant color changes whereas with "Rainbow" the difference is barely noticable)

VectorPlot[vectorfield, {x, -1, 1}, {y, -1, 1},
VectorPoints -> {20, 20},
VectorScale -> {Automatic, Automatic, If[#5 > 2, 0, #5] &},
RegionFunction -> ((tempa^2 < (#1^2 + #2^2) < 1 && #2 < 0) &),
VectorColorFunction -> Hue,
VectorColorFunctionScaling -> False]


you get the desired output:

-