# Second (non-linear) X-Axis

**URL:** <https://discourse.julialang.org/t/second-non-linear-x-axis/117082>\
**Category:** Visualization\
**Tags:** plotting, plots\
**Created:** [July 16, 2024, 12:25am UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082 "2024-07-16T00:25:47Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jprenderg](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jprenderg](https://discourse.julialang.org/u/jprenderg)\
**Post date:** [July 16, 2024, 12:25am UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/1 "2024-07-16T00:25:47Z")

</div>

Hello,

Does anyone know how to make a plot with a second x-axis (on the top edge of the plot). I’d like to make values of the second x-axis a user supplied function of values on the primary x-axis.

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [July 16, 2024, 7:45am UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/3 "2024-07-16T07:45:59Z")

</div>

Welcome! Are you using Plots.jl or Makie.jl?

---

<div class="post-metadata">

**Author:** ![jprenderg](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jprenderg](https://discourse.julialang.org/u/jprenderg)\
**Post date:** [July 16, 2024, 9:26am UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/4 "2024-07-16T09:26:11Z")

</div>

Hello! I generally use Plots.jl but have used Makie.jl a bit too.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 16, 2024, 9:54am UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/5 "2024-07-16T09:54:27Z")

</div>

With Plots.jl check `twiny()`:  
Example:

```julia
using Plots
x = 1:10000
plot(x, log.(x), c=:blue, x_foreground_color_axis=:blue, x_foreground_color_text=:blue)
p2 = twiny()
plot!(p2, log.(x/2), 2*log.(x), c=:red, x_foreground_color_axis=:red, x_foreground_color_text=:red)

```

 ![Plots_twiny_colored_ticks](https://global.discourse-cdn.com/julialang/original/3X/2/8/283ce7b568f5734f1587a54b1f0dbf902117bc6b.png)

---

<div class="post-metadata">

**Author:** ![jprenderg](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jprenderg](https://discourse.julialang.org/u/jprenderg)\
**Post date:** [July 16, 2024, 10:38am UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/6 "2024-07-16T10:38:24Z")

</div>

Thanks. I should have been more specific. I’m trying to plot just one trace. The bottom x-axis should be in the original units of x. The top x-axis should be in units of some function of x, say log(x).

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 16, 2024, 10:52am UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/7 "2024-07-16T10:52:45Z")

</div>

NB: removed proposal, as well pointed out by @jules, the f(x) scale was not correct.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 16, 2024, 11:34am UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/8 "2024-07-16T11:34:18Z")

</div>

That doesn’t look correct, a log scale and a linear scale can’t both have ticks at equal distances if they are about the same data.

If you want to use the same ticks as the lower axis, just transformed, you generally won’t get nice numbers on the log scale. If the upper axis can have different tick positions than the lower one, there are more choices.

---

<div class="post-metadata">

**Author:** ![jprenderg](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jprenderg](https://discourse.julialang.org/u/jprenderg)\
**Post date:** [July 16, 2024, 11:38am UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/9 "2024-07-16T11:38:32Z")

</div>

Yes, I’m just trying to do one plot say for y = x. Then have the top x-axis be a function of the bottom x-axis. For example, if the transformation function is 1/x and the bottom x is 5, the top x would be .2.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 16, 2024, 12:06pm UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/10 "2024-07-16T12:06:36Z")

</div>

Thanks @jules, indeed there is a problem.

I remember a few years ago I was looking for this feature and at that time I had to use PyPlot.jl. See the [PyPlot solution here](https://discourse.julialang.org/t/plots-with-a-secondary-non-linear-axis-scale/56661), where the function f(x) must be monotonic.

---

<div class="post-metadata">

**Author:** ![jprenderg](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jprenderg](https://discourse.julialang.org/u/jprenderg)\
**Post date:** [July 16, 2024, 1:08pm UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/11 "2024-07-16T13:08:13Z")

</div>

Thank you! It’s the empirical PyPlot solution I was looking for.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 16, 2024, 1:23pm UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/12 "2024-07-16T13:23:45Z")

</div>

Here a simple solution with Plots.jl, really simple:

```julia
using Plots; gr(dpi=600)
x = 1:1000; y = log.(x)
f(x) = 1/x
p1 = plot(x, y, c=:blue, xlabel="x", ylabel="y")
xt = xticks(p1)[1][1]
str2 = string.(round.(f.(xt), digits=5))
plot!(twiny(), x, y, c=:blue, xticks=(xt, str2), xlabel="f(x) scale")

```

 ![Plots_twiny_single_curve](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed267dd6182c9d3340f8162e6fc6ae4a0fc8ae44.png)

---

<div class="post-metadata">

**Author:** ![jprenderg](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jprenderg](https://discourse.julialang.org/u/jprenderg)\
**Post date:** [July 16, 2024, 1:50pm UTC](https://discourse.julialang.org/t/second-non-linear-x-axis/117082/13 "2024-07-16T13:50:30Z")

</div>

Exactly what I was looking for. Thank you.
