# How to control color bar min and max in StreamPlot

I have the following code

R = 0.5
\[Alpha] = 90
x2 = Cos[\[Alpha] Degree] R
y2 = Sin[\[Alpha] Degree] R
EField[x_, y_, x0_, y0_, q_] =
q {x - x0, y - y0}/((x - x0)^2 + (y - y0)^2)

StreamPlot[
EField[x, y, -R, 0, 2] + EField[x, y, x2, y2, 6], {x, -R*1.1,
R*1.1}, {y, -R*1.1, R*1.1}, PlotLegends -> Placed[Automatic, Below]]



Which provides the following plot:

You can see because of the asymptotes it is taking a maximum value that is so large that all other values become blue. Is there a way to manually change the colorbar range so that the min and max are more reasonable and thus more detail can be seen?

I've tried to add a PlotLegends-> BarLegend[...] via this link but it doesn't seem to change the actual plot. Any advice is appreciated. :)

• We cannot reproduce your plot since you have not defined x2 or y2 Commented Jan 15, 2022 at 19:17
• Whoops, sorry bob. It was inside of a Manipulate and so I tried to include just the relevant part. I'll update now. Thank you. Commented Jan 15, 2022 at 19:25
• Should be correct now. Commented Jan 15, 2022 at 19:28
• I'm not going to change it at this point. But if anyone wants to copy this in the future for the purpose of displaying electric fields, please note: I did not divide the electric field formula but the radius and so it is incorrect. It should be divided by ((x - x0)^2 + (y - y0)^2)^(3/2) Commented Jan 16, 2022 at 14:54

One method would be to use a logarithmic scale for the arrow colors. Unfortunately it seems that StreamPlot does not support ScalingFunctions, but you can build your own color scale and legend by slightly adapting this answer,

{min,max}={0.3,990};
sf=Log[#/min]/Log[max/min]&;
isf=InverseFunction@sf;

StreamPlot[
EField[x, y, -R, 0, 2] + EField[x, y, x2, y2, 6],
{x, -R*1.1, R*1.1}, {y, -R*1.1, R*1.1},
StreamColorFunctionScaling->False,
StreamColorFunction->Function[ColorData["Rainbow"][sf@#5]],
PlotLegends->
BarLegend[{"Rainbow",{min,max}},
ScalingFunctions->{sf,isf},
ColorFunctionScaling -> True
]
]


• Thanks Hausdorff, this should work for what I need! Cheers! Commented Jan 16, 2022 at 13:54