5

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, -R1.1, R1.1}, {y, -R1.1, R1.1}, PlotLegends -> Placed[Automatic, Below]]

Which provides the following plot:

enter image description here enter image description here

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. :)

akozi
  • 853
  • 3
  • 10
  • 1
    We cannot reproduce your plot since you have not defined x2 or y2 – Bob Hanlon Jan 15 '22 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. – akozi Jan 15 '22 at 19:25
  • Should be correct now. – akozi Jan 15 '22 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) – akozi Jan 16 '22 at 14:54

1 Answers1

7

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, -R1.1, R1.1}, {y, -R1.1, R1.1}, StreamColorFunctionScaling->False, StreamColorFunction->Function[ColorData["Rainbow"][sf@#5]], PlotLegends-> BarLegend[{"Rainbow",{min,max}}, ScalingFunctions->{sf,isf}, ColorFunctionScaling -> True ] ]

enter image description here

Hausdorff
  • 3,505
  • 1
  • 9
  • 25