5
$\begingroup$

I have a problem with putting arrowheads on my parametric plot, I have looked through the various examples on here, which all work fine, but when I replace it with the code for my parametric plot, I get the error:

Arrow setbacks VertexColors -> Automatic should be a number or pair of numbers.

For example,

This code works to put an arrow halfway along a plot which is what i would like to do on my own plot

    ClearAll[f, x, data];
    f[x_] := x^2;
    data = Table[{x, f[x]}, {x, -10, 10, 0.1}];
    p0 = ListPlot[data, Frame -> True, Axes -> False, Joined -> True, 
       PlotStyle -> {Blue, Thick}];


    p0 /. Line[x_] :> {Arrowheads[{0,0.1,0}], Arrow[x]}

and produces

enter image description here

However, when i change it over to my plot:

   p0 = ParametricPlot[{x, y}, {t, 0, pt[[1]]}, PlotStyle -> Thick, 
   PlotRange -> {{-0.8, 0.8}, {-0.8, 0.8}}, 
   ColorFunction -> Function[{x, y, t}, Hue[0.7*t]], Frame -> True, 
   FrameLabel -> {{"y/λ", None}, {"x/λ", 
      HoldForm["f" = Fi]}}, 
   GridLines -> {{0.4, 0, -0.4}, {-0.4, 0, 0.4}}, 
   FrameTicksStyle -> Thick, 
   FrameTicks -> {{{-0.8, -0.4, 0, 0.4, 0.8}, 
      None}, {{-0.8, -0.4, 0, 0.4, 0.8}, None}}, 
   GridLinesStyle -> Opacity[0.5], Axes -> False, ImageSize -> 300, 
   PlotRangePadding -> None, BaseStyle -> {FontSize -> 18}, 
   AspectRatio -> Automatic];


   p0 /. Line[x_, y_] :> {Arrowheads[{0, 0.1, 0}], Arrow[x, y]}

i get this:

enter image description here

For the life of me i can't figure this out, the plot appears but is covered in the dreaded red box with the error message i gave at the top of the page.

$\endgroup$
  • $\begingroup$ The complete code has not been provided. For instance, x, y, and pt are undefined. Hence, ParametricPlot fails. $\endgroup$ – bbgodfrey Dec 16 '15 at 6:17
  • $\begingroup$ It is defined in my code, those come from an NDS solve of four coupled differential equations, i didn't include the whole thing because it's a big block of work. You can replace x and y with Cos[t] and Sin[t] and pt[[1]] with 2Pi and you still get the same error $\endgroup$ – Colin Dec 16 '15 at 7:07
7
$\begingroup$

The real problem your are having is the way GraphicsComplex uses VertexColors. In short, your parametric plot with colors produces something like this

Graphics[GraphicsComplex[{{0, 0}, {1, 1}, {2, 1}},
  {Line[{1, 2, 3}, VertexColors -> Automatic]}, 
  VertexColors -> {Yellow, Blue, Green}
  ]]

Mathematica graphics

Note that at the end of Line it says VertexColors -> Automatic which states that it uses the vertex color definition that is given at the end of GraphicsComplex.

Just replacing Line with Arrow will therefore not work and you will get an error message because Arrow does not take a VertexColors option.

In fact, Arrow cannot be used with VertexColors at all. Fortunately, Arrow cannot take only points as arguments, but Lines primitives itself. Therefore, one solution to your problem is

p0 = ParametricPlot[{t, t^2}, {t, 0, 1}, PlotStyle -> Thick, 
  PlotRange -> {{-0.8, 0.8}, {-0.8, 0.8}}, 
  ColorFunction -> Function[{x, y, t}, Hue[0.7*t]], Frame -> True, 
  FrameLabel -> {{"y/\[Lambda]", None}, {"x/\[Lambda]", 
     HoldForm["f" = Fi]}}, 
  GridLines -> {{0.4, 0, -0.4}, {-0.4, 0, 0.4}}, 
  FrameTicksStyle -> Thick, 
  FrameTicks -> {{{-0.8, -0.4, 0, 0.4, 0.8}, 
     None}, {{-0.8, -0.4, 0, 0.4, 0.8}, None}}, 
  GridLinesStyle -> Opacity[0.5], Axes -> False, ImageSize -> 300, 
  PlotRangePadding -> None, BaseStyle -> {FontSize -> 18}, 
  AspectRatio -> Automatic]

p0 /. l_Line :> {Arrowheads[{0, 0.1, 0}], Arrow[l]}

Mathematica graphics

$\endgroup$
  • $\begingroup$ Beautiful mate thanks :) $\endgroup$ – Colin Dec 16 '15 at 7:56
3
$\begingroup$

In the absence of definitions for x, y, and pt, set

 x = t^2; y = t^3;

and replace p[[1]] by 1 for specificity. Then an arrow is obtained by replacing the last line of the code by

(p0 // Normal) /. Line[x_] :> {Arrowheads[{0, 0.1, 0}], Arrow[x]}

enter image description here

Normal is needed to convert GraphicsComplex to Line.

$\endgroup$
  • $\begingroup$ Thank you, this works nicely, however, the Hue color function of the line has been lost (or overwritten?). $\endgroup$ – Colin Dec 16 '15 at 7:20
  • $\begingroup$ @Colin It has been lost but can be recovered with a little work. Too late for me to do so now, however. $\endgroup$ – bbgodfrey Dec 16 '15 at 7:21
  • $\begingroup$ No worries thanks for your work, if anyone could come up with a work around to get the color function working again i'd be very greatfull :) $\endgroup$ – Colin Dec 16 '15 at 7:29

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.