Change xlabel position

How can I adjust the position of the x-label in a plot using Plots.jl? e.g. if I do

using Plots
gr() # making it explicit I'm using the GR backend

plot(1:10, rand(1:2, 10))
xlabel!("my nice xlabel")

I get a plot with the xlabel centred:

test

I want to change the x-position of the x-label. If I try to add text centred around x = 7 using annotate!:

plot(1:10, rand(1:2, 10))
annotate!(7, 0.8, "my nice xlabel") # guessing y would be 0.8 for the text, based on the y-axis

I get a plot with no text, because annotate! only seems to add text to the figure area bounded by the axes:

test

(whereas

annotate!(7, 1, "my nice xlabel")

adds text because y ∈ [1, 2], and y position for the text ∈ [1, 2]):

test

Are there any specific arguments I need to pass to annotate! in order for it to plot text in places outside the axes, or is there a more direct way of controlling the position of the x-label?

In the example the annotation is not showing up because it is outside of the figure margins. This works:

julia> using Plots, Plots.Measures

julia> plot(1:10, rand(1:2, 10),margin=1cm)

julia> annotate!(7, 0.8, "my nice xlabel")


image

2 Likes

Lovely, thank you!