# Plot ticks at both top and bottom axis

**URL:** <https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550>\
**Category:** Visualization\
**Tags:** plotting, plots\
**Created:** [March 6, 2018, 9:38pm UTC](https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550 "2018-03-06T21:38:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaslusimoes](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kaslusimoes](https://discourse.julialang.org/u/kaslusimoes)\
**Post date:** [March 6, 2018, 9:38pm UTC](https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550/1 "2018-03-06T21:38:45Z")

</div>

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](https://matplotlib.org/examples/axes_grid/simple_axisline4.html) from Matplotlib.

Thanks!

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 6, 2018, 9:50pm UTC](https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550/2 "2018-03-06T21:50:54Z")

</div>

I don’t think so

---

<div class="post-metadata">

**Author:** ![kaslusimoes](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kaslusimoes](https://discourse.julialang.org/u/kaslusimoes)\
**Post date:** [March 6, 2018, 10:08pm UTC](https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550/3 "2018-03-06T22:08:13Z")

</div>

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…)

---

<div class="post-metadata">

**Author:** ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)\
**Post date:** [March 6, 2018, 10:17pm UTC](https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550/4 "2018-03-06T22:17:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kaslusimoes](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kaslusimoes](https://discourse.julialang.org/u/kaslusimoes)\
**Post date:** [March 7, 2018, 1:01pm UTC](https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550/5 "2018-03-07T13:01:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)\
**Post date:** [February 5, 2019, 7:09pm UTC](https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550/6 "2019-02-05T19:09:10Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![Pablo\_Marchant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_marchant/32/32828_2.png) [@Pablo\_Marchant](https://discourse.julialang.org/u/Pablo_Marchant)\
**Post date:** [May 1, 2022, 5:45pm UTC](https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550/7 "2022-05-01T17:45:31Z")

</div>

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](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.

```julia
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:

- code for twiny ([plotly - Julia: two x axes for plot of same data - Stack Overflow](https://stackoverflow.com/questions/64176617/julia-two-x-axes-for-plot-of-same-data))
- code to remove labels on twin axes ([Avoid repeating tick labels with subplots in Plots.jl](https://discourse.julialang.org/t/avoid-repeating-tick-labels-with-subplots-in-plots-jl/351))

![sine](https://global.discourse-cdn.com/julialang/original/3X/4/9/491ce2e7a44eb1c1d083f7eebf81daa36aa74f19.png)

---

<div class="post-metadata">

**Author:** ![Pablo\_Marchant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_marchant/32/32828_2.png) [@Pablo\_Marchant](https://discourse.julialang.org/u/Pablo_Marchant)\
**Post date:** [May 1, 2022, 6:13pm UTC](https://discourse.julialang.org/t/plot-ticks-at-both-top-and-bottom-axis/9550/8 "2022-05-01T18:13:11Z")

</div>

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

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/1/b/1baca1b956fb19563270291f1bd3a0baa7f799d8.png)
