5
$\begingroup$

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:

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

$\endgroup$
4
  • 1
    $\begingroup$ We cannot reproduce your plot since you have not defined x2 or y2 $\endgroup$
    – Bob Hanlon
    Jan 15 at 19:17
  • $\begingroup$ 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. $\endgroup$
    – akozi
    Jan 15 at 19:25
  • $\begingroup$ Should be correct now. $\endgroup$
    – akozi
    Jan 15 at 19:28
  • $\begingroup$ 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) $\endgroup$
    – akozi
    Jan 16 at 14:54

1 Answer 1

7
$\begingroup$

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

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks Hausdorff, this should work for what I need! Cheers! $\endgroup$
    – akozi
    Jan 16 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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