Message Boards Message Boards

2
|
7461 Views
|
4 Replies
|
8 Total Likes
View groups...
Share
Share this post:

ColorFunction problems with StreamDensityPlot [SOLVED]

Posted 11 years ago
It seems there's some problem when using ColorFunction with a StremDensityPlot and with its options on StreamPoints:
I'm trying to create a demonstration (much more complex than the simple example posted here) in wich there are the field lines of an electric dipole and in the background there's a color representing the field intensity. But it seems that the StreamPoints settings affect the rendering of the background color (set through the option ColorFunction):

With this code
 FieldPts =
   Flatten[{Table[{{xq1 + d Cos[\[Alpha]], yq1 + d Sin[\[Alpha]]},
        RGBColor[1, 1, 0]}, {\[Alpha], 0, 2 \[Pi] + 0.1, \[Pi]/(3 4)}],
       Table[{{xq2 + d Cos[\[Alpha]], yq2 + d Sin[\[Alpha]]},
        RGBColor[1, 1, 0]}, {\[Alpha], 0, 2 \[Pi] + 0.1, \[Pi]/(
        3 4)}]}, 1] /. {xq1 -> -2, yq1 -> 0, xq2 -> 2, yq2 -> 0,
     qq1 -> 2, qq2 -> -2, s -> 2, d -> 0.7};
 mycolfunc[z_] :=
  GrayLevel[
  2/\[Pi] ArcTan[z]]; StreamDensityPlot[{-((
    2 (-2 + x))/((-2 + x)^2 + y^2)^(3/2)) + (
   2 (2 + x))/((2 + x)^2 + y^2)^(
   3/2), -((2 y)/((-2 + x)^2 + y^2)^(3/2)) + (2 y)/((2 + x)^2 + y^2)^(
   3/2)}, {x, -7, 7}, {y, -7, 7}, ColorFunction -> mycolfunc,
ColorFunctionScaling -> False, StreamPoints -> FieldPts]
I get this image:

That's ok.

But if I add some other setting to the StreamPoint command (in the form of "{spec,dspec,len}" to specify a minimum distance between streamlines and a maximum length for any streamline)...
 FieldPts =
   Flatten[{Table[{{xq1 + d Cos[\[Alpha]], yq1 + d Sin[\[Alpha]]},
        RGBColor[1, 1, 0]}, {\[Alpha], 0, 2 \[Pi] + 0.1, \[Pi]/(3 2)}],
       Table[{{xq2 + d Cos[\[Alpha]], yq2 + d Sin[\[Alpha]]},
        RGBColor[1, 1, 0]}, {\[Alpha], 0, 2 \[Pi] + 0.1, \[Pi]/(
        3 2)}]}, 1] /. {xq1 -> -2, yq1 -> 0, xq2 -> 2, yq2 -> 0,
     qq1 -> 2, qq2 -> -2, s -> 2, d -> 0.7};
 mycolfunc[z_] :=
  GrayLevel[
  2/\[Pi] ArcTan[z]]; StreamDensityPlot[{-((
    2 (-2 + x))/((-2 + x)^2 + y^2)^(3/2)) + (
   2 (2 + x))/((2 + x)^2 + y^2)^(
   3/2), -((2 y)/((-2 + x)^2 + y^2)^(3/2)) + (2 y)/((2 + x)^2 + y^2)^(
   3/2)}, {x, -7, 7}, {y, -7, 7}, ColorFunction -> mycolfunc,
ColorFunctionScaling -> False, StreamPoints -> {FieldPts, 1, 10}]
... then I get this

In this latter case the field intensity is clearly not as expected (it seems as there are cubes seen in perspectives... (?) )

Anyway I need the second kind of code to be able to set the number, length and density of the field lines.

I wonder why a simple change in this option can change the behaviour of the ColorFunction result.
The only working solution I could find was to overlap the StreamDensityPlot with a blank DensityPlot with just the ColorFunction option in it. But I think that's not efficient and it can slow down my demonstration (in which there will be the possibility to move the source charges).

Any help, suggestion?

[Edit - 11/11/2013: I can consider my question as [Solved] with Shedelbower's hint about using the MaxRecursion->2 (or more) option]
POSTED BY: Luca M
4 Replies
Luca,

Include the option MaxRecursion->2. Experiment with the value to achieve the desired result.
POSTED BY: Tim Shedelbower
Posted 11 years ago
Thanks Shedelbower

That made it!

I didn't know about MaxRecursion (yet I searched the documentation a lot -- you never, never know enough with Mathematica options!).
MaxRecursion->2 makes the background acceptable. Setting it to a slightly higher value (i.e. 4) gives a better graphics result (at the expense of some more delay in the front-end presentation).

Here's the code I made for showing the different results with MaxRecursion set at 0,1,2,4.
It seems that, without an explicit setting of that option, the  ColorFunction in StremDensityPlot assumes a default value of 0 for MaxRecursion if the StreamPoint command is in the form of "{spec,dspec,len}".

Here's the code...
 FieldPts =
   Flatten[{Table[{{xq1 + d Cos[\[Alpha]], yq1 + d Sin[\[Alpha]]},
        RGBColor[1, 1, 0]}, {\[Alpha], 0, 2 \[Pi] + 0.1, \[Pi]/(3 2)}],
       Table[{{xq2 + d Cos[\[Alpha]], yq2 + d Sin[\[Alpha]]},
        RGBColor[1, 1, 0]}, {\[Alpha], 0,
        2 \[Pi] + 0.1, \[Pi]/(3 2)}]}, 1] /. {xq1 -> -2, yq1 -> 0,
     xq2 -> 2, yq2 -> 0, qq1 -> 2, qq2 -> -2, s -> 2, d -> 0.7};
 mycolfunc[z_] := GrayLevel[2/\[Pi] ArcTan[z]]; Table[
  StreamDensityPlot[{-((2 (-2 + x))/((-2 + x)^2 + y^2)^(3/2)) + (2 (2 +
          x))/((2 + x)^2 + y^2)^(3/
        2), -((2 y)/((-2 + x)^2 + y^2)^(3/2)) + (2 y)/((2 + x)^2 +
        y^2)^(3/2)}, {x, -7, 7}, {y, -7, 7},
  ColorFunction -> mycolfunc, ColorFunctionScaling -> False,
  StreamPoints -> {FieldPts, 1, 10}, ImageSize -> 400,
  MaxRecursion -> k,
  PlotLabel ->
   Style["MaxRecursion \[Rule] " <> ToString[k], 18, Bold]], {k, {0,
   1, 2, 4}}]
... and here's the corresponding result...
POSTED BY: Luca M
Dear Luca,

Probably it is better to use DensityPlot and StreamPlot separately and combine them using Show[plot1,plot2]. 
POSTED BY: Sander Huisman
Posted 11 years ago
Thanks Sander
Your solution is exactly the one I ended up with by myself, before reading your helpful answer, and it works.

Anyway it seems there is some misbehaviour in Mathematica's StreamDensityPlot command, unless I'm missing something about it.
POSTED BY: Luca M
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract