# VectorPlot "Part 1 of {} does not exist" error

Bug introduced in 7.0, persisting through 13.2.

I am looking to plot the Magnetic Vector potential, A, as a function of position along the edge of a density plot. The density plot is fine, and I would combine the two plots using Show. After searching, I found a similar question here. I needed to modify the code a bit to get it to work in version 12.2, however the following code works just fine:

ClearAll
μ = 0.02; Px = -0.5; h = 1.0;
vData = Table[{1, j}, {j, -1, 1, 0.1}];
f[y_] := (h^2/(2 μ)) (-Px) (1 - (y/h)^2)
VectorPlot[{f[y], 0}, {x, 0, 3}, {y, -h, h}, VectorPoints -> vData,
VectorScaling -> Automatic, VectorColorFunction -> "Rainbow",
PlotRange -> {{0, 4}, {-1.1, 1.1}},
Epilog -> {{Red, Thick, Line[{{0, 1}, {4, 1}}]}, {Red, Thick,
Line[{{0, -1}, {4, -1}}]}}]


And results in this plot:

However, when I substitute my own code, with as far as I can tell the same approach, I get a "Part::partw: Part 1 of {} does not exist." error. Besides a different function form, I do not see anything different between my error prone code and the working code above. Any guidance would be much appreciated! Thanks.

ClearAll
a = 5 10^-3;
q1 = 1.602 * 10^-10;
eps0 = 8.85418781 * 10^-12;
qEnclosed[r_] := If[r < a,  (q1 r^3)/a^3, q1];
eField[r_] := qEnclosed[r]/(4 π r^2 eps0);
potSphereTEMP[r_] = -Integrate[eField[r], r ];
potInfinity = Limit[potSphereTEMP[r], {r -> ∞} ];
potSphere[r_] :=
potSphereTEMP[r] -
potInfinity  (* assume potential is zero at infinity *)
vData = Table[{0, j}, {j, -1.5 a, 1.5 a, 0.15 a}]
VectorPlot[{potSphere[y], 0}, {x, -a, a}, {y, -1.5 a, 1.5 a},
VectorPoints -> vData, VectorScaling -> Automatic,
VectorColorFunction -> "Rainbow",
PlotRange -> {{-a, a}, {-1.5 a, 1.5 a}}]

• welcome to Mathematica Stackexchange. In your second code chunk you have eField and eField1. Is this intentional? Otherwise eField1 is undefined. Commented Jun 25, 2023 at 2:21
• Oddly, this seems to work (replacing efield1 by efield): VectorPlot[{potSphere[y], 0}/1, {x, -a, a}, {y, -1.5 a, 1.5 a}, VectorPoints -> vData, VectorScaling -> Automatic, VectorColorFunction -> "Rainbow", PlotRange -> {{-a, a}, {-1.5 a, 1.5 a}}] Commented Jun 25, 2023 at 3:04
• The problem is found before, but doesn't receive enough attention: mathematica.stackexchange.com/q/81182/1871 It's broken from the beginning: i.sstatic.net/1R7iU.png Commented Jun 25, 2023 at 6:36
• This bug of StreamPlot is essentially the same bug, I believe: mathematica.stackexchange.com/q/133381/1871 Commented Jun 25, 2023 at 6:46
• Yeah, you can report bug via e-mail: wolfram.com/support/contact/?topic=feedback They reply fairly soon. Commented Jun 25, 2023 at 12:45

It might be a bug. I'm not sure why it makes a difference -- something to do with Piecewise maybe -- but using ?NumericQ seems to avoid the problem:

ClearAll[potSphere]; (***)
a = 5 10^-3;
q1 = 1.602*10^-10;
eps0 = 8.85418781*10^-12;
qEnclosed[r_] := If[r < a, (q1 r^3)/a^3, q1];
eField[r_] := qEnclosed[r]/(4 \[Pi] r^2 eps0);
potSphereTEMP[r_] = -Integrate[eField[r], r];
potInfinity = Limit[potSphereTEMP[r], {r -> \[Infinity]}];
potSphere[r_?NumericQ] :=  (***)
potSphereTEMP[r] -
potInfinity ;(*assume potential is zero at infinity*)
vData = Table[{0, j}, {j, -1.5 a, 1.5 a, 0.15 a}];
VectorPlot[{potSphere[y], 0}/1, {x, -a, a}, {y, -1.5 a, 1.5 a},
VectorPoints -> vData, VectorScaling -> Automatic,
VectorColorFunction -> "Rainbow",
PlotRange -> {{-a, a}, {-1.5 a, 1.5 a}}]


Here's simple example of the bug, which seems to center on a vector List in which a component contains Piecewise for unknown reasons:

VectorPlot[{Piecewise[{{y, y > 0}}], 1},
{x, -1, 1}, {y, -1, 1}]


Output is a pink graphic with a "Part is not a Graphics primitive or directive" message. The graphics generated are:

Graphics[{{}[[1]], {}},...<options>...]


These work:

1. Head not List (see also this comment):
 VectorPlot[Identity@{Piecewise[{{y, y > 0}}], 1},
{x, -1, 1}, {y, -1, 1}]

1. Head Piecewise, value a List (e.g. PiecewiseExpand[{Piecewise[{{y, y > 0}}], 1}]):
VectorPlot[Piecewise[{{{0, 1}, y <= 0}}, {y, 1}],
{x, -1, 1}, {y, -1, 1}]


These two produce the following graphics:

• Its a bug, that even the usual Evaluate of the Plot object does not handle. Include the second coordinate into the definition potSphere[r_] := {potSphereTEMP[r] - potInfinity , 0}. VectorPlot[ potSphere[y] , ..., Authors of graphic routines expect bite-sized flat-numeric functions. Its the weak point of the LISP philosophy "everything is a list" Commented Jun 25, 2023 at 4:26
• @MichaelE2 didn’t have opportunity to run (just noting code anomaly) but this answer has been instructive in terms of Mathematica behaviour. Commented Jun 25, 2023 at 5:37