juliaplots.jljulia-plots

How can I change the arrow size in a plot?


I am trying to change the size of an arrow.
In the function arrow I can set headlength and headwidth but this does not change the arrow size in the plot.
How can I change the arrow size in a plot?
Julia: v1.10.4
Plots: v1.40.5

using Plots

plot([0,1], [0,1], label="Bool", arrow=true)
plot!([0,1], [0,.8], label="Closed", arrow=:closed)
plot!([0,1], [0,.6], label="Closed lw5", lw=5, arrow=:closed)
plot!([0,1], [0,.4], label="Closed size0.9", arrow=(:closed, 0.9))
plot!([0,1], [0,.2], label="arrow", arrow=arrow(:closed, 0.9))

arrow()
#Plots.Arrow(:simple, :head, 0.3, 0.3)

arrow(:closed, 0.9)
#Plots.Arrow(:closed, :head, 0.9, 0.9)

Image showing lines with arrows


Solution

  • Looking at the Plots.Arrow API page it says we can pass up to four arguments -- the style of the arrow, the side you want the arrow on, the length of the arrowhead, and the width of the arrowhead (I think you figured this out already based on your code but putting in answer for posterity's sake).

    Unfortunately, despite arrows being listed as supported by the default GR backend it appears not all arrow attributes in that API are.

    The easiest way to "fix" this is to just switch which backend you use. i.e. doing:

    using Plots
    pythonplot() #switch from default GR to python backend
    #old way to switch to python backend was `pyplot()` -- thanks to @user9711258 for pointing out `pythonplot()` is now the preferred syntax
    plot([0,1],[0,1], label="args = :closed, :head, 1., 1.", arrow=(Plots.arrow(:closed,:head,1.,1.)))
    plot!([0,1.],[0,2.], label="args = :closed, :head, 5., 3.", arrow=(Plots.arrow(:closed,:head,5.,3.)))
    plot!(legend=:topleft)
    

    produces the desired behavior: julia plot with different sized arrows

    If you run the same code without changing the backend first the arrow sizes don't change...sadly a lot of things (especially geometry related) don't work in the default GR backend yet. If you are really set on using GR to do this you will (as of this writing) have to draw the arrows yourself. This is actually not so hard but does require a bit of extra work. I can show you how to do this if you want to go that route but the easier fix that works how the API says it should is to just switch backends to one that supports all the arrow attributes.