logo

Drawing Arrows in Graphics with Julia 📂Julia

Drawing Arrows in Graphics with Julia

Code

  • plot!([x1, x2], [y1, y2], arrow=:true)

This code plots an arrow from point $(x1, y1)$ to point $(x2, y2)$ on the plot. Naturally, the tip of the arrow is at the terminal point $(x2, y2)$. The maximum value of the sine function can be shown as follows.

using Plots

x = range(0, 2π, 100)
plot(x, sin.(x), label="", ylims=(-1.3,1.3))
plot!([π/2, 3], [1, 1.1], arrow=:true, color=:black, label="")
annotate!(3.7, 1.1, "maximum")

Arrow tip

The style of the tip can be chosen as :open or :closed.

  • Not specified or :true: a polyline $\to$
plot!([3π/2, 3], [-1, -1.1], arrow=:open, color=:red, label="")
annotate!(2.3, -1.1, "minimum")

Arrow direction

The direction of the tip can be set to :head, :tail, :both, with :head being the default.

plot!([π/2, π/2], [0, 1], arrow=(:closed, :both), color=:purple, label="")
annotate!(0.75π, 0.5, "amplitude")

In the official documentation1, there are explanations about the headlength and headwidth options, but trying to use them only leads to errors and it is unclear how to use them.

Environment

  • OS: Windows11
  • Version: Julia 1.9.0, Plots v1.38.12