2
$\begingroup$

I am trying to modify the thickness, color, style, etc. of vectors in a VectorDensityPlot.

After some testing, I realised my problem was caused by having:

  • Replacement rules in the vector field expression
  • A function e.g. F[A_,y_,z_] = {A z, -A y} of parameters that I specified in the plotting command, e.g. F[2,y0,z0].

I copy here a minimal code that reproduces this issue. What is the reason for this behaviour?

VectorDensityPlot[
 ({A z0, -A y0} /. {A -> 2}),
 {y0, -1, 1},
 {z0, -1, 1},
 VectorStyle -> {"Drop", Red}
 ] 

ignores the VectorStyle options and uses the defaults, while

VectorDensityPlot[
 ({2 z0, -2 y0} ),
 {y0, -1, 1},
 {z0, -1, 1},
 VectorStyle -> {"Drop", Red}
 ] 

works.

VectorDensityPlot results

EDIT: As noted by a comment from user Henrik Schumacher, adding Evaluate[] to the vector field fixes the problem. I would very much like to understand what is the reason for this.

$\endgroup$
  • $\begingroup$ What version of Mathematica are you using? I have had issues with plotting specifications on older versions (sub ~11.2). $\endgroup$ – Haff May 17 '18 at 19:01
  • 2
    $\begingroup$ I don't know for sure why this happens, but using Evaluate seems to make it working: VectorDensityPlot[ Evaluate[({A z0, -A y0} /. {A -> 2})], {y0, -1, 1}, {z0, -1, 1}, VectorStyle -> {"Drop", Red} ] $\endgroup$ – Henrik Schumacher May 17 '18 at 19:02
  • $\begingroup$ This happens in both Mathematica 10 and 11.3. $\endgroup$ – Francisco Rodríguez Fortuño May 21 '18 at 14:11
  • 1
    $\begingroup$ I don't know why this works in this particular case. But it is often a good idea to fully evaluate an expression before it is sent to plotting commands. $\endgroup$ – Henrik Schumacher May 21 '18 at 14:17
  • 1
    $\begingroup$ I've reported it internally. $\endgroup$ – rcollyer May 21 '18 at 14:27
1
$\begingroup$

The first argument has a Head of ReplaceAll, where as the second is List

({A z0, -A y0} /. {A -> 2})

is full form is

ReplaceAll[List[Times[A, z0], Times[Times[-1, A], y0]], List[Rule[A, 2]]]

At the time to assign Styles, Mathematica checks is the various elements to plot match the structure of the Style elements, so they can be assigned one-to-one.

I think the problem is similar to this example:

Here there is a list of three expressions to plot, matching the list of three PlotStyle directives.

Plot[
 {x, 2 x, 3 x}
 , {x, 0, 1}
 , PlotStyle -> {Red, Green, Blue}
 ]

enter image description here

on the contrary, this other example there is an expression with a Head of Times that does NOT match the PlotStyle option structure.

Plot[
 x Range[3]
 , {x, 0, 1}
 , PlotStyle -> {Red, Green, Blue}
 ]

Mathematica graphics

Using Evaluate forces the expression to be evaluated into a structure that matches both the argument and the Style.

| improve this answer | |
$\endgroup$

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.