I’d classify that as at least an unhandled edge case. To work around in Plots
using Plots
gr()
"""
arrow_segment_annot!(p, x1, y1, x2, y2; color=:red, lw=2, arrowsize=10)
Draw a line from (x1,y1) to (x2,y2) and put an arrowhead at (x2,y2)
as a rotated Unicode arrow annotation. Works even with xflip=true.
"""
function arrow_segment_annot!(p, x1, y1, x2, y2; color=:red, lw=2, arrowsize=10)
# Draw the line
plot!(p, [x1, x2], [y1, y2]; lc=color, lw=lw, label=nothing)
# Compute angle in degrees in data space
θ = atan(y2 - y1, x2 - x1) * 180 / π
# Add an arrowhead annotation at the end
annotate!(p, x2, y2, text('➤', arrowsize, color, rotation=θ))
return p
end
# Example with flipped x-axis
p = plot(sin, 0, 2π; lw=2, label="", xlim=(0, 2π), xflip=true)
arrow_segment_annot!(p, 1, -0.5, π, 0.0; color=:red, lw=2, arrowsize=12)
display(p)
However, you can do this with less drama using CairoMakie
using CairoMakie
f(x) = sin(x)
fig = Figure(resolution = (600, 400))
ax = Axis(fig[1, 1], xlabel = "x", ylabel = "y")
xs = range(0, 2π; length = 400)
lines!(ax, xs, f.(xs))
origin = Point2f(1, -0.5)
target = Point2f(π, 0.0)
direction = target - origin
arrows2d!(
ax,
[origin],
[direction];
color = :red,
shaftwidth = 2,
tipwidth = 10,
tiplength = 15,
)
ax.xreversed = true
fig