7
$\begingroup$

Consider this simple streamline plot:

StreamPlot[{-y/10, x/10}, {x, -1, 1}, {y, -1, 1}, 
 StreamColorFunction -> Function[{x, y, u, v}, Hue[v]]]

enter image description here

The arguments to the colour function are rescaled to lie between 0 and 1, so the colours span the whole spectrum, as expected.

But now I want to turn off colour function scaling and work with the original vector field values. Those lie between -1/10 and 1/10, so I should get only hues between purple and orange:

StreamPlot[{-y/10, x/10}, {x, -1, 1}, {y, -1, 1}, 
 StreamColorFunction -> Function[{x, y, u, v}, Hue[v]], 
 StreamColorFunctionScaling -> False]

enter image description here

I'm sorry, Dave. I'm afraid I can't do that.

Is this a bug? How do I work around it?

$\endgroup$
  • 1
    $\begingroup$ If I Print out the values of x, y, u and v that are being passed to the color function, v is not x/10, which is surprising. $\endgroup$ – rm -rf May 15 '14 at 17:39
  • 2
    $\begingroup$ The scaling is "affected" by turning off StreamColorFunctionScaling, but not in the correct way. It's definitely not a behavior that a sane person would expect. But Mathematica is probably thinking it's doing what's best for the mission... $\endgroup$ – Jens May 15 '14 at 17:42
  • 1
    $\begingroup$ Thanks for pointing this out. I've reported it, and we'll take a look. $\endgroup$ – rcollyer May 15 '14 at 20:57
  • 1
    $\begingroup$ "I'm sorry, Dave." LOL $\endgroup$ – Mr.Wizard May 15 '14 at 21:36
  • $\begingroup$ @rcollyer: You should take a look at ChadK's answer. As far as I can tell, the bug is that Mathematica's behaviour is inconsistent with its documentation. $\endgroup$ – user484 May 15 '14 at 22:03
8
$\begingroup$

The StreamColorFunction has different arguments than you expect. It is actually like this: {x, y, u/um, v/um, um}, where $um = Sqrt[u^2+v^2]$.

So all you need to do is redefine your function to account for this:

StreamPlot[{-y/10, x/10}, {x, -1, 1}, {y, -1, 1}, 
 StreamColorFunction -> Function[{x, y, u, v, um}, Hue[u*um]], 
 StreamColorFunctionScaling -> False]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Welcome to the site, and thanks for your contribution. Please consider selecting a more "human" name, by the way. $\endgroup$ – Mr.Wizard May 15 '14 at 21:52
  • $\begingroup$ But what if I rescale everything by choosing the plot function and range as {-y/100,x/100},{x,0,10},{y,0,10}? It's wrong again. I don't think the order you're assuming works correctly. $\endgroup$ – Jens May 15 '14 at 21:55
  • $\begingroup$ It looks like this works. Thanks! $\endgroup$ – user484 May 15 '14 at 22:01
  • $\begingroup$ Yes, it works after correcting the order of arguments. $\endgroup$ – Jens May 15 '14 at 22:07
  • 2
    $\begingroup$ What are the chances that someone signed up just to answer my question? I feel honoured. :) $\endgroup$ – user484 May 15 '14 at 22:20
7
$\begingroup$

A work-around:

vv = {};
StreamPlot[{-y/10., x/10.}, {x, -1, 1}, {y, -1, 1},
  StreamColorFunction -> (AppendTo[vv, #4] &)];

With[{MinMax = Through[{Min, Max}[vv]]},
 StreamPlot[{-y/10., x/10.}, {x, -1, 1}, {y, -1, 1}, 
  StreamColorFunction -> (Hue[Rescale[#4, MinMax, {-.1, .1}]] &)]
]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ You should probably find the Min and Max values of vv outside of the Function, and inject them; as written this runs that part repeatedly. It may or may not matter but it seems like bad style. $\endgroup$ – Mr.Wizard May 15 '14 at 21:37
  • $\begingroup$ I am going to take the liberty of making the change described above. If you don't like it you can revert it. $\endgroup$ – Mr.Wizard May 15 '14 at 21:40
  • $\begingroup$ @Mr.Wizard, Just noticed you comment. Thank you for the edit. $\endgroup$ – kglr May 15 '14 at 21:43

Your Answer

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