coloring streamlines to and from origin vector field plot

I wrote the simple following code to plot a given vector field:

rr := Sqrt[x^2 + y^2];
a := 0.5;
xlim = 1;
splot = StreamPlot[{
rr^a (x (x rr + y rr + y^2))/rr^3,
rr^a (y (x rr + y rr - x^2))/rr^3},
{x, -xlim, xlim}, {y, -xlim, xlim},
StreamColorFunction -> "Heat", AxesLabel -> {"x", "y"}];
Show[splot]


I would like to color the trajectories which enter and leave the origin in different colors. For example:

Is there a simple way to do this? Thanks very much in advance!

This should get you started:

First, make a color function that uses the point $(x,y)$ and vector $(v_x,v_y)$ at that point to get the $\cos$ of the angle between the two:

Clear[color]
color[x_, v_] := Module[{q},
q = x.v/(Norm[x] Norm[v]);
Return[Blend[{Blue, Black, Red}, (q + 1)/2]]
]


Edit Another color Function:

Clear[color]
color[x_, v_] := Module[{q, c},
Which[
x[[1]] > 0 && x[[2]] <= 0,
c = GrayLevel[.2]
,
True,
q = x.v/(Norm[x] Norm[v]);
c = Blend[{Blue, Black, Red}, (q + 1)/2]
];
Return[c]
]


Then use the function in your plot:

rr := Sqrt[x^2 + y^2];
a := 0.5;
xlim = 1;
splot = StreamPlot[{rr^a (x (x rr + y rr + y^2))/rr^3,
rr^a (y (x rr + y rr - x^2))/rr^3},
{x, -xlim, xlim}, {y, -xlim, xlim},
StreamColorFunctionScaling -> False,
StreamColorFunction -> (color[{#1, #2}, {#3, #4}] &),
AxesLabel -> {"x", "y"}];
Show[splot]


You can play with the colors in the Blend[] function to get the look what you want.

• Thank you -- this is almost exactly what I needed! One small question, how can it be modified so that ll curves under the x-axis are blue. I didn't describe this well, but I only want to color the exiting curves from the origin red (as in my hand-drawn picture) Nov 1, 2017 at 23:49
• You almost never need to use Return[], and Normalize[] is built-in. Thus: color[x_, v_] := Blend[{Blue, Black, Red}, (1 + Normalize[x].Normalize[v])/2]. Alternatively: color[x_, v_] := Blend[{Blue, Black, Red}, 1 - CosineDistance[x, v]/2]. Nov 2, 2017 at 4:12
• Thanks! Is there a way to modify the color function to make it piecewise defined so that it follows this rule above the x-axis and below it colors everything blue? Nov 2, 2017 at 11:43
• Actually, I'd like all the vectors in the fourth quadrant to be colored gray. Nov 2, 2017 at 14:38
• I was able to do this simply by "showing" two plots, where I excluded the relevant regions. Thanks! Nov 2, 2017 at 15:24