Plot ticks at both top and bottom axis

Hi,

Is there any way to plot both ‘top’ and ‘bottom’ x axis ticks using Plots.jl?

I’m thinking of something like this example from Matplotlib.

Thanks!

2 Likes

I don’t think so

Wow that definitely was not the answer I was expecting haha

Do you see any way we could implement this feature? Would it be something that a newbie like me could try programming? Or is it too much low-level?

(I even tried some things with twinx() but couldn’t succeed…)

I think you could use PyPlot. Basically it is a wrapper for matplotlib. Hence, you should be able to do everything you do on matplotlib if I’m right. Take a look at PyPlot.jl

Thanks, I guess that’s indeed my best option right now

1 Like

But is there an option just to show the ticks on the top and right axis (without labels and numbers and the same interval as the left and bottom axis)?

This is an old post but something I stumbled with today and took me a bit to find a fix. Copying here something I put on an associated issue in Plots.jl github page (https://github.com/JuliaPlots/Plots.jl/issues/2218), just in case it can help someone.

Not a proper fix, but here is a workaround I found. I just create a twin axis in each direction, match the x and y boundaries and remove labels using a copy_ticks function.

function twiny(sp::Plots.Subplot)
    sp[:top_margin] = max(sp[:top_margin], 30Plots.px)
    plot!(sp.plt, inset = (sp[:subplot_index], bbox(0,0,1,1)))
    twinsp = sp.plt.subplots[end]
    twinsp[:xaxis][:mirror] = true
    twinsp[:background_color_inside] = RGBA{Float64}(0,0,0,0)
    Plots.link_axes!(sp[:yaxis], twinsp[:yaxis])
    twinsp
end
twiny(plt::Plots.Plot = current()) = twiny(plt[1])

function copy_ticks(sp::Plots.Subplot)
    ptx = twinx(sp)
    plot!(ptx,xlims=xlims(plt),ylims=ylims(plt),xformatter=_->"",yformatter=_->"")
    pty = twiny(sp)
    plot!(pty,xlims=xlims(plt),ylims=ylims(plt),xformatter=_->"",yformatter=_->"")
end
copy_ticks(plt::Plots.Plot = current()) = copy_ticks(plt[1])

plt = plot(sin)
copy_ticks()
display(plt)
savefig("sine.png")

This was built with code from two sources:

sine

Just realized the linked example is not exactly what I show here, but it can be easily adapted to do something similar to that matplotlib example

using Plots

function twiny(sp::Plots.Subplot)
    sp[:top_margin] = max(sp[:top_margin], 30Plots.px)
    plot!(sp.plt, inset = (sp[:subplot_index], bbox(0,0,1,1)))
    twinsp = sp.plt.subplots[end]
    twinsp[:xaxis][:mirror] = true
    twinsp[:background_color_inside] = RGBA{Float64}(0,0,0,0)
    Plots.link_axes!(sp[:yaxis], twinsp[:yaxis])
    twinsp
end
twiny(plt::Plots.Plot = current()) = twiny(plt[1])

function copy_ticks(sp::Plots.Subplot,xticks)
    ptx = twinx(sp)
    plot!(ptx,xlims=xlims(plt),ylims=ylims(plt),yformatter=_->"",xformatter=_->"", grid=false)
    pty = twiny(sp)
    plot!(pty,xlims=xlims(plt),ylims=ylims(plt),yformatter=_->"",xticks=xticks, grid=false)
end
copy_ticks(plt::Plots.Plot,xticks) = copy_ticks(plt[1],xticks)

plt = plot(sin, xlims=(0,2π))
copy_ticks(current(),([0,π/2,π,3π/2,2π],["0","π/2","π","3π/2","2π"]))
display(plt)
savefig("sine.png")

sine

4 Likes