Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
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. –  rm -rf May 15 at 17:39
2  
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... –  Jens May 15 at 17:42
1  
Thanks for pointing this out. I've reported it, and we'll take a look. –  rcollyer May 15 at 20:57
1  
"I'm sorry, Dave." LOL –  Mr.Wizard May 15 at 21:36
    
@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. –  Rahul Narain May 15 at 22:03
show 1 more comment

2 Answers 2

up vote 7 down vote accepted

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

share|improve this answer
    
Welcome to the site, and thanks for your contribution. Please consider selecting a more "human" name, by the way. –  Mr.Wizard May 15 at 21:52
    
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. –  Jens May 15 at 21:55
    
It looks like this works. Thanks! –  Rahul Narain May 15 at 22:01
    
Yes, it works after correcting the order of arguments. –  Jens May 15 at 22:07
2  
What are the chances that someone signed up just to answer my question? I feel honoured. :) –  Rahul Narain May 15 at 22:20
show 1 more comment

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

share|improve this answer
    
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. –  Mr.Wizard May 15 at 21:37
    
I am going to take the liberty of making the change described above. If you don't like it you can revert it. –  Mr.Wizard May 15 at 21:40
    
@Mr.Wizard, Just noticed you comment. Thank you for the edit. –  kguler May 15 at 21:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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