Hello I'm trying to manually clip the scale of the colors for the streamlines in StreamPlot. The problem is that at the origin the velocity goes to infinity and that throws off any attempt to rescaling. I've already tried modifying the RegionFunction... to no avail.

This is the default output:

StreamPlot[{-(y/(2 \[Pi] (x^2 + y^2))), x/(2 \[Pi] (x^2 + y^2))}, {x, -3, 3}, {y, -3, 3}, PlotLegends -> Automatic]

Stream Plot with color issue

I have managed to somewhat achieve a working example:

StreamPlot[{-(y/(2 \[Pi] (x^2+y^2))),x/(2 \[Pi] (x^2+y^2))},{x,-3,3},{y,-3,3},RegionFunction->Function[{x,y,vx,vy,n},x^2 +y^2>0.1],PlotLegends->Automatic,StreamColorFunction->(ColorData["Rainbow"][Rescale[Norm[{#3,#4}],{0,0.00005}]]&),StreamColorFunctionScaling->False]

Rescaled colors on StreamPlot, missing colorbar

However I still had to fiddle with the Rescale values, 0.00005 is somewhat arbitrary and it does not tell me what is the actual value assigned to red or above. The plotlegend (ColorBar) then has to be inserted manually and I don't know how to do that. What I would like is a simple command to say: "Set Color Range to -> {vmin,vmax}" in this case the values I'm interested are {0,0.35} and then display the color bar with correct values as well. Thank you!


2 Answers 2


One possible solution

bar = BarLegend[{(ColorData["Rainbow", #] &), {1/(2 Pi Sqrt[3^2] ), 
    1/(2 Pi Sqrt[0.15^2])}}]; StreamPlot[{-(y/(2 \[Pi] (x^2 + y^2))), 
  x/(2 \[Pi] (x^2 + y^2))}, {x, -3, 3}, {y, -3, 3}, 
 StreamColorFunction -> (ColorData["Rainbow", 1/( Norm[{#1, #2}])] &),
  PlotLegends -> Placed[bar, Below], StreamPoints -> 30, 
 StreamColorFunctionScaling -> False]

Figure 1

  • $\begingroup$ This works well in version 12.3. I want to understand what is the code actually doing. What are the inputs at the BarLegend, and how did you arrive at the {1/(2 Pi Sqrt[3^2]), 1/(2 Pi Sqrt[0.15^2])} values? $\endgroup$
    – Michel G
    Commented Jan 17, 2022 at 19:57
  • $\begingroup$ Also, how do StreamColorFunction-> (ColorData["Rainbow", 1/(Norm[{#1, #2}])] &) and our previously defined Bar legend will interact? Thank you! $\endgroup$
    – Michel G
    Commented Jan 17, 2022 at 19:59
  • $\begingroup$ @MichelG There are 3 functions to visualize streamlines in a given color data "Rainbow". First is StreamPoints -> 30 (we using it instead of RegionFunction) that makes empty region around singularity x=0, y=0. Second is StreamColorFunction that generates color of streamlines with a rule of vector field singularity. And the last one is PlotLegends that makes bar legend with the scale of the field variation on this picture. $\endgroup$ Commented Jan 18, 2022 at 1:41

If your plot spans several orders of magnitude, you might also consider using a logarithmic scale, as in these examples.

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

  {-(y/(2 \[Pi] (x^2 + y^2))), x/(2 \[Pi] (x^2 + y^2))}, 
  {x, -3, 3}, {y, -3, 3}, 
  StreamColorFunctionScaling -> False, 
  StreamColorFunction -> Function[ColorData["Rainbow"][sf@#5]], 
  PlotLegends -> 
    BarLegend[{"Rainbow", {min, max}}, ScalingFunctions -> {sf, isf}, 
    ColorFunctionScaling -> True]

enter image description here

The options ScalingFunctions and ColorFunctionScaling are not officially supported for BarLegend, and are therefore higlighted red in notebooks (which was also seen in the answers of this post). However, at least in versions 12.0 and 13.0 they still work, as they are options of the internal Charting`iBarLegend.

  • $\begingroup$ It is a nice solution (+1). It looks like in version 13 there is support for arbitrary scaling function as it shown in my answer. $\endgroup$ Commented Jan 17, 2022 at 10:08
  • $\begingroup$ This looks very practical and useful, pretty much what I need, however BarLegend in my version (12.3) doesn't recognize those functions. I will see if I'm able to update my version. Thank you! $\endgroup$
    – Michel G
    Commented Jan 17, 2022 at 19:51
  • $\begingroup$ @MichelG In version 12.2 it seems to work fine. Mathematica highlights ScalingFunctions and ColorFunctionScaling as invalid options for BarLegend, though they seem to be doing their job, as the result is the same as in version 13.0.Maybe they are just undocumented $\endgroup$
    – Hausdorff
    Commented Jan 17, 2022 at 23:40
  • $\begingroup$ @AlexTrounev Thanks :) (though it is essentially a copy of my other answer) I am not sure I understand your point about the scaling function, though. After all, for Streamplot I am essentially doing the same thing as you just with a logarithmic function in ColorFunctionScaling, right? $\endgroup$
    – Hausdorff
    Commented Jan 17, 2022 at 23:47
  • $\begingroup$ @Hausdorff I like your approach. But why it is working in v.12.3 with a message InverseFunction::ifun: Inverse functions are being used. Values may be lost for multivalued inverses.? Also as Michel mentioned there is red color for ScalingFunctions and ColorFunctionScaling. Fortunately I have v.13, and there is also same message. But plot has been generated in v12.3 and v13 as well. $\endgroup$ Commented Jan 18, 2022 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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