PGFPlotsX howto annotate?

Hello,

I’m moving to using PGFPlotsX, for it’s suitability for producing good publication ready plots, and got following the PGFPlotsX.jl and PGFPlots documentation most that I need functioning.

However I got stuck in figuring out how to annotate a plot with a text to illustrate a specific point. I’d appreciate a hint on how to do that in a simple way (and would be nice to get that on the PGFPlotsX.jl examples).

Follows a minimum working example, however with the annotation missing:

using PGFPlotsX

x_in = range(-100/π, stop = 100.0/π, length = 100);

@pgf p = Axis({ xmin = x_in[1]
              , xmax = x_in[end]
             },
          Plot({ 
                color = "blue"
               },
               Table( x_in, @. abs(sin(x_in)/x_in) )
              ),
          HLine({ dashed, blue }, 0.3),
          VLine({ dashed, red }, -15.0),
          #now I want to put a text somewhere, say "this is the red line", Howto?
          )

Thanks in advance for your help,
Rob.

Your best option is usually finding an existing example in pgfplots (ie LaTeX code), the translating it to the syntax of PGFPlotsX.

If you need help, just please link an example.

Dear Tamas,

  • That’s (looking into pgfplots (LaTeX)) what I did, and what didn’t help me.
  • No good LaTeX example caught my eye → so I put a M(not)WA in the post.
  • I expected that annotating a plot would be a regularly occurring thing to do for the PGFPlotX users in Julia and so a hint on how to do so rather obvious. But apparently not…

→ I’ll keep searching.

1 Like

raw"\node[anchor = south west] at (-10, .4) {this is the red line};"

3 Likes

Thanks Johanni,

that was a useful answer! I didn’t expect one would have to use the raw command for such a common use case.

Note that raw is just a string literal which allows you to input strings without a lot of escape \s. It does not have anything to do with PGFPlotsX, you could do

"\\node[..."

and get the same result. Just that because of all the LaTeX code, raw"..." is easier.

There is currently no good julia interface in PGFPlotsX.jl for the node stuff (https://github.com/KristofferC/PGFPlotsX.jl/issues/36) so, for now, one needs to use raw strings.

3 Likes

We introduced some new syntax that makes it easier to write \node commands in LaTeX with computed information. (Otherwise, if you are manually specifying it, @jbrea’s solution is simpler). Slightly tweaking the example to demo this,

using PGFPlotsX
x_in = range(-100/π, stop = 100.0/π, length = 1000);
coords = (-15.0, 0.3)
@pgf p = Axis({ xmin = x_in[1], xmax = x_in[end], ymax = 1.2 },
              Plot({ color = "blue" },
                   Table(x_in, @. abs(sin(x_in)/x_in))),
              VLine({ dashed, red }, coords[1]),
              HLine({ dashed, blue }, coords[2]),
              [raw"\node",
               { anchor = "south west", pin = "0:this is the red line" },
               " at ", Coordinate(coords[1], 1.1), "{};"])

produces

5 Likes