Add tag with coordinates to plot in Plots.jl

I have a plot with intersecting lines:
plot

And I would like to add a tag or pointer which point to the intersection and display the coordinates of the intersection. Something like this:
tag example

I know the coordinates of the intersection and I know that the function annotate! adds text to the plot. However, I do not know how to add an line or arrow to point to the intersection and how to determine the right position for the text.

Here’s the start of a hack if you want one:

plot([(1, 0), (0, 1)]; arrows=true, texts=["hi",""])

@gustaphe’s suggestion is good. Just remember that the coordinates are data-relative (not plot display-relative). So zooming in will make the marker bigger.

Alternative using single-point dataset & marker:

You can add the marker as a simple shape that will resize itself when you zoom in. But I don’t see a way to offset the text in a display-relative manner either. Your best bet is probably to specify the alignment of the text as :bottom, :left, or something (see below):

pt = (3,9) #where to place the marker

#Add a single point using marker formatted as desired:
plot!([pt[1]],[pt[2]], markershape=:plus,
	markercolor=:grey, markerstrokecolor=:black, markerstrokewidth=3,
	markersize=10
)

#Annotate with x-value
ptstr = string(pt)
xvalstr = string(pt[1]) #Just x value
annotate!((pt...,text(xvalstr, 14, :bottom, :left, :green)))

image

If you don’t need add these markers in an automated fashion, the InspectDR backend allows you to add them using the r and d keys (keyboard).

  • r: reference marker: a single point with (x,y) coords displayed.
  • d: The delta marker: couples with the last reference marker to calculate the distance & slope between them (box can be moved with click+drag).

(more info on mouse/key bindings here:)
https://github.com/ma-laforge/InspectDR.jl/blob/master/doc/input_bindings.md

image

Note:

  • The top-left marker is a single marker generated with r while hovering over the plot.
  • The 2 markers on the right were created with the r-d key combination.

Limitation

I don’t think there is an API yet to generate these markers programmatically, but it probably can be added without requiring a much new code (you can open a PR if this interests you).

And since you are working with frequency plots, you might be interested in knowing that InspectDR allows you to stack multiple y strips that maintains a common frequency window as you zoom in interactively.

This is ideal for Bode plots (see below)

Limitation

At the moment, this feature does not work with Plots.jl’s linked axes system (only works with stacked y strips; not plots with linked x axes).